<?php
class Shepherd_ShipmentController extends Sss_Controller_Shepherd
{
	private $_menuName = 'Shipments';
	private $_menuNameInventory = 'Hardware Management';
	public function init()
	{
		parent::init();
		$this->checkInstance();
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		if (!$userInformation->username) {
			$this->_redirect("/$handle/user/login");
		}
	}
	public function indexAction()
	{
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$this->_redirect("/$handle/shipment/shipments");
	}
	
	public function shipmentsAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$this->getValueRequest('shipmentname', '', Sss_Ultility_SessionName::SHIPMENT_SESSION);
		$get  = $this->getRequest()->getParams();
		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;

		$isSearch = isset($get['s']) && $get['s'] == 1;
		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::VISIBLE, 'feature', 'action' ,$this->listRole(), true) == 1 ){
			$shipmentTable = new Shepherd_Model_Mapper_Shipment();
			$alertTable    = new Shepherd_Model_Mapper_Alert();
			$productTable  = new Shepherd_Model_Mapper_Product();
			$documentTable = new Shepherd_Model_Mapper_Document();
			$hardwareTable = new Application_Model_Mapper_Hardware();


			$stockLocationTable = new Application_Model_Mapper_StockLocation();
			$allocationTable = new Shepherd_Model_Mapper_Allocation(); 

			//get order, page, limit param
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD] = isset($get[Sss_Ultility_CommonViewParamater::ORDERFIELD]) ? $get[Sss_Ultility_CommonViewParamater::ORDERFIELD] : null;
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE]  = isset($get[Sss_Ultility_CommonViewParamater::ORDERTYPE]) ? $get[Sss_Ultility_CommonViewParamater::ORDERTYPE] : null;
			$filterParam[Sss_Ultility_CommonViewParamater::PAGE] = isset($get[Sss_Ultility_CommonViewParamater::PAGE]) ? $get[Sss_Ultility_CommonViewParamater::PAGE] : 1;
			//test param filter: omit title text in field
			$paramOriginal        =  $isSearch ? $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'origin', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';
			$paramDestination     = $isSearch ? $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'destination', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';
			$paramFromDate     = $isSearch ? strcmp($this->getRequest()->getParam('date_from', Sss_Ultility_CommonViewParamater::UNDERFINE), '') == 0 ? '' : $this->getRequest()->getParam('date_from', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';
			$paramToDate       = $isSearch ? strcmp($this->getRequest()->getParam('date_to', Sss_Ultility_CommonViewParamater::UNDERFINE), '') == 0 ? '' : $this->getRequest()->getParam('date_to', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';
			$paramInternalRef     = $isSearch? $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'internal_ref', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';
			$paramSupplierRef     = $isSearch ? $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'supplier_ref', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';
			$paramReferenceNumber = $isSearch ? $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'reference_number', Sss_Ultility_CommonViewParamater::UNDERFINE) : '';



			//get shipment filter
			$filterParam['origin']            = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'origin', trim($paramOriginal), Sss_Ultility_SessionName::SHIPMENT_SESSION);
			$filterParam['destination']      = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'destination', trim($paramDestination), Sss_Ultility_SessionName::SHIPMENT_SESSION);
			$filterParam['date_from']        = $this->getValueRequest('date_from', $paramFromDate, Sss_Ultility_SessionName::SHIPMENT_SESSION);
			$filterParam['date_to']          = $this->getValueRequest('date_to', $paramToDate, Sss_Ultility_SessionName::SHIPMENT_SESSION);
			$filterParam['internal_ref']     = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'internal_ref', trim($paramInternalRef), Sss_Ultility_SessionName::SHIPMENT_SESSION);
			$filterParam['supplier_ref']     = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'supplier_ref', $paramSupplierRef, Sss_Ultility_SessionName::SHIPMENT_SESSION);
			$filterParam['reference_number'] = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'reference_number', trim($paramReferenceNumber), Sss_Ultility_SessionName::SHIPMENT_SESSION);

			if ($userInformation->username) {
				//delete shipment
				$shipmentId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_ID, -1);
				if (isset($shipmentId) && $shipmentId != -1) {
					//get alert list for shipment
					$listAlert           = $alertTable->getList(Sss_Ultility_SystemName::SHIPMENTS,$shipmentId);

					//get product list for shipment
					$listProduct           = $productTable->getList($shipmentId);

					//get document list for shipment
					$listDocument           = $documentTable->getList($shipmentId);

					//get sensor list for shipment
					$listSensor           = $hardwareTable->getListByDestination($shipmentId, Sss_Ultility_SystemName::SHIPMENTS);
					if(count($listAlert) > 0 || count($listProduct) > 0 || count($listDocument) > 0 || count($listSensor) > 0 ){
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('PROPERTIES_EXIST_TO_SHIPMENT'));     
					}else{
						$deleteId      = $shipmentTable->deleteRow($shipmentId,$instanceId);
						if (isset($deleteId)) {
							$arrDelete = array(
							'destination = ?' => (int)$shipmentId,
							'systemname = ?' => Sss_Ultility_SystemName::SHIPMENTS);
							$deleteId      = $allocationTable->deleteRow(null,$arrDelete,$instanceId);
							$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_SHIPMENT_SUCCESSFULL'));
						} else {
							$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_SHIPMENT_FAIL'));
						} 
					}
				}
				//delete item
				$itemTable       = new Shepherd_Model_Mapper_Item();
				$deleteItemId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_ITEM_ID, -1);
				if (isset($deleteItemId) && $deleteItemId != -1) {
					$deleteId = $itemTable->deleteRow((int) $deleteItemId,$instanceId);
					if (isset($deleteId)) {
						$arrId = array(
						'destination = ?' => (int) $deleteItemId,
						'systemname = ?' => Sss_Ultility_SystemName::ITEMS
						);
						$allocationTable->deleteRow(null,$arrId,$instanceId);
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_ITEM_SUCCESSFULL'));
					} else {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_ITEM_FAIL'));
					}
				}
				//delete dynamic Monitoring
				$shipmentDeleteId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_SHIPMENT_ID, -1);
				if (isset($shipmentDeleteId) && $shipmentDeleteId != -1) {
					$arrId    = array(
					'destination = ?' => (int) $shipmentDeleteId
					);
					$deleteId = $allocationTable->deleteRow(null,$arrId,$instanceId);
					if (isset($deleteId)) {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_SHIPMENT_SUCCESSFULL'));
					} else {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_SHIPMENT_FAIL'));
					}
				}
				//delete alert
				$deleteAlertId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_ALERT_ID, -1);
				if (isset($deleteAlertId) && $deleteAlertId != -1) {
					$deleteId   = $alertTable->deleteRow((int) $deleteAlertId,$instanceId);
					if (isset($deleteId)) {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_ALERT_SUCCESSFULL'));
					} else {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_ALERT_FAIL'));
					}
				}
			}
			$params = $this->getRequest()->getPost();
			if ($userInformation->username) {
				//update hardware when detach
				if(isset($params['hardwareid']) && isset($params[Sss_Ultility_CommonViewParamater::BTN_SAVE])){
					$hardwareIds = $params['hardwareid'];
					$stockLocationIds =  $params['stocklocationid'];

					$listHardwareOfShipment = $hardwareTable->getListByDestination($params['shipmentid'], Sss_Ultility_SystemName::SHIPMENTS);

					$shipId = intval($params['shipmentid']);
					$id;
					try{
						foreach ($listHardwareOfShipment as $h) {
							$getAllocationId = $allocationTable->getById($shipId,Sss_Ultility_SystemName::SHIPMENTS,null,(int) $h['hardwareid']);
							if($getAllocationId) {
								$allocationUpdate = $allocationTable->findById($getAllocationId->allocationid,$instanceId);
								$allocationUpdate->setStatus(0);
								$allocationUpdate->setNote(@$params['txtLocation'][$shipId]);
								$allocationTable->save($allocationUpdate,$instanceId);
							} 
							$hardwareUpdate = $hardwareTable->findById((int) $h['hardwareid']);
							$hardwareUpdate->setDetachLocationName(@$params['txtLocation'][$shipId]);
							$hardwareUpdate->setDetachLocationId(@$params['txtOrigin'][$shipId]);
							$id   = $hardwareTable->save($hardwareUpdate);
						} 
						if(isset($params['shipmentid']) && $params['shipmentid'] > 0) {
							$endDate = Sss_Ultility_Common::getDateTimeNumber(empty($params['txtEnddate'][$shipId])?time():$params['txtEnddate'][$shipId]);
							$shipmentUpdate = $shipmentTable->findById(intval($params['shipmentid']),$instanceId);
							$shipmentUpdate->setEndDate($endDate);
							$shipDate = date(DATETIME_MYSQL_FORMAT,$shipmentUpdate->shipDate);
							$id   = $shipmentTable->save($shipmentUpdate,$instanceId);
							//update field enddt in table allocations
							if(empty($params['txtEnddate'][$shipId])) {
								$date = new DateTime();
								$enddt = $date->format(DATETIME_MYSQL_FORMAT);
							} else {
								$date = explode("/",$params['txtEnddate'][$shipId]);
								$str = $date[1] . '/' . $date[0].'/'.$date[2];
								$enddt = date(DATETIME_MYSQL_FORMAT,strtotime($str));
							}
							$allocationTable->updateEnddtByDestination(intval($params['shipmentid']), $shipDate,$enddt,$instanceId);
							// End update
						}
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DETACH_SENSOR_SUCCESSFULL'));
					}catch (exception $e){
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DETACH_SENSOR_FAIL'));
					}
				}
			}
			$filterParam['shipment_history'] = 0;
			//get shipment list
			$checkShipmentSession = new Zend_Session_Namespace('checkShipment');
			$checkDynamicSession = new Zend_Session_Namespace('checkDynamic');
			if (isset($get['shipment']) || isset($get['dynamicMonitoring'])) {
				$checkShipmentSession->checkShipment = (int) $get['shipment'];
				$checkDynamicSession->checkDynamic = (int) $get['dynamicMonitoring'];
			} else {
				if (!isset($checkShipmentSession->checkShipment)) {
					$checkShipmentSession->checkShipment = 1;
				}
				if (!isset($checkDynamicSession->checkDynamic)) {
					$checkDynamicSession->checkDynamic = 1;
				}
			}
			$checkShipment = $checkShipmentSession->checkShipment;
			$checkDynamic = $checkDynamicSession->checkDynamic;
			if($checkShipment && !$checkDynamic) {
				$list       = $shipmentTable->getList($this->view->limit, 0, $filterParam);
				$row        = $list->list;
			}elseif($checkDynamic && !$checkShipment) {
				$list       = $shipmentTable->getListItemShipmentDynamicMonitoring($this->view->limit, 0, $filterParam,0);
				$row        = $list->list;
			}else{
				$list       = $shipmentTable->getListItemShipmentDynamicMonitoring($this->view->limit, 0, $filterParam,1);
				$row        = $list->list;
			}
			// Save session checkShipment and checkDynamic
			$this->view->assign(Sss_Ultility_CommonViewParamater::CHECK_SHIPMENT,$checkShipment);
			$this->view->assign(Sss_Ultility_CommonViewParamater::CHECK_DYNAMIC_MONITORING,$checkDynamic);
			$listAlertShipment    = array();
			$listProductShipment  = array();
			$listDocumentShipment = array();
			$listSensorShipment   = array();
			foreach ((array) $row as $r) {
				//get alert list for shipment
				$listAlert           = $alertTable->getList(Sss_Ultility_SystemName::SHIPMENTS, $r['shipmentid']);
				$listAlertShipment[] = count($listAlert);

				//get product list for shipment
				$listProduct           = $productTable->getList($r['shipmentid']);
				$listProductShipment[] = count($listProduct);

				//get document list for shipment
				$listDocument           = $documentTable->getList($r['shipmentid']);
				$listDocumentShipment[] = count($listDocument);

				//get sensor list for shipment
				$listSensor           = $hardwareTable->getListByDestination($r['shipmentid'], Sss_Ultility_SystemName::SHIPMENTS);
				$listSensorShipment[] = $listSensor;
			}
			//get alert list for dynamic
			$listAlertDynamic = $alertTable->getList(Sss_Ultility_SystemName::SHIPMENTS);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_ALERT_DYNAMIC, $listAlertDynamic);

			//get alert list for item
			$listAlertItem = $alertTable->getList(Sss_Ultility_SystemName::ITEMS);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_ALERT_ITEM, $listAlertItem);
			
			//get stock location 
			$stockLocation = $stockLocationTable->getAllStockLocation();
			$ArrStockLocation = array();
			$dataAuto = '';
			foreach($stockLocation as $sl)
			{
				$ArrStockLocation[$sl['stocklocationname']]= $sl['stocklocationid'];
			}
			foreach ($ArrStockLocation as $key=>$value) {
				$dataAuto = $dataAuto."$key|$value#";
			}
			$dataAuto = substr($dataAuto, 0 ,strlen($dataAuto) - 1);
			$this->view->assign(Sss_Ultility_CommonViewParamater::AUTOCOMPLETE, $dataAuto);
			$countSensor = 0;
			foreach($listSensorShipment as $ls) {
				foreach($ls as $s) {
					$countSensor = $countSensor + 1;
				}
			}
			//get sensor list by item
			$hardwareByItem = $hardwareTable->getHardwareByDestinationGraph('', Sss_Ultility_SystemName::ITEMS);
			$chartsByItem = array();
			$index = 0;
			foreach ($hardwareByItem as $tmp) {
				$chartIds = '';
				$chartList =    $hardwareTable->getChartIds($tmp['hardwareid'], $tmp['systemtypeid']);
				if($chartList){
					foreach($chartList as $c){
						if($chartIds == ''){
							$chartIds = $c['chart'];
						}else{
							$chartIds = $chartIds . ',' .  $c['chart'];
						}
					}
				}

				$tmp['chart'] = $chartIds;
				$chartsByItem[$index]     = $tmp;
				$index = $index + 1;
			}
			$this->view->assign(Sss_Ultility_CommonViewParamater::HARDWARE_BY_ITEM, $hardwareByItem);
			$this->view->assign(Sss_Ultility_CommonViewParamater::HARDWARE_BY_ITEM_LIST, $chartsByItem);
			//get sensor list by shipment
			$hardwareByShipment = $hardwareTable->getHardwareByDestinationGraph('', Sss_Ultility_SystemName::SHIPMENTS);
			$chartsByShipment = array();
			$index = 0;
			foreach ($hardwareByShipment as $tmp) {
				$chartIds = '';  
				$chartList =    $hardwareTable->getChartIds($tmp['hardwareid'], $tmp['systemtypeid']);
				if($chartList){
					foreach($chartList as $c){
						if($chartIds == ''){
							$chartIds = $c['chart'];
						}else{
							$chartIds = $chartIds . ',' .  $c['chart'];
						}
					}
				}
				$tmp['chart'] = $chartIds; 
				$chartsByShipment[$index]     = $tmp;
				$index = $index + 1;
			}
			$this->view->assign(Sss_Ultility_CommonViewParamater::HARDWARE_BY_SHIPMENT, $chartsByShipment);
			if($countSensor == 0){
				$countSensor = count($row);
			}
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM_COUNT, $countSensor);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_ALERT_SHIPMENT, $listAlertShipment);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_PRODUCT_ITEM, $listProductShipment);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_DOCUMENT_ITEM, $listDocumentShipment);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM, $listSensorShipment);

			$pagination = $list->pagination;
			$this->view->assign(Sss_Ultility_CommonViewParamater::FILTER_PARAM, $filterParam);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $row);
			$this->view->assign(Sss_Ultility_CommonViewParamater::PAGINATION, $pagination);
			$orderQueryString = '';
			if ($isSearch) {
				$orderQueryString = '&' . Sss_Ultility_CommonViewParamater::PAGE . '=' . $filterParam[Sss_Ultility_CommonViewParamater::PAGE] . '&s=1';
			} else {
				$orderQueryString = '&' . Sss_Ultility_CommonViewParamater::PAGE . '=' . $filterParam[Sss_Ultility_CommonViewParamater::PAGE];
			}
			$queryString = '';
			if ($isSearch) {
				$queryString .= '&s=1';
			}
			if (!is_null($filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD])){
				$queryString .= '&' . Sss_Ultility_CommonViewParamater::ORDERFIELD . '=' . $filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD];
			}
			if (!is_null($filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE])){
				$queryString .= '&' . Sss_Ultility_CommonViewParamater::ORDERTYPE . '=' . $filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE];
			}
			$this->view->assign(Sss_Ultility_CommonViewParamater::QUERY_STRING, $queryString);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ORDER_QUERY_STRING, $orderQueryString);
			$actionbuttons = array(
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::ADD, 'add-shipment', Resources::_('ADD_NEW_SHIPMENT')),
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::ADDITEM, 'add-item', Resources::_('ADD_NEW_ITEM')),
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::EXPORT, 'shipment-export?export=1', Resources::_('EXPORT')),
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::PRINTF, 'shipment-export?print=1', Resources::_('PRINT')),
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::SEARCH, 'javascript:hideShow(this);', Resources::_('SEARCH'))
			);
			//check permission
			if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::ADD, 'feature', 'action' ,$this->listRole(), true) != 1 )
			{
				$actionbuttons = array(
				new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::EXPORT, 'shipment-export?export=1', Resources::_('EXPORT')),
				new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::PRINTF, 'shipment-export?print=1', Resources::_('PRINT')),
				new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::SEARCH, 'javascript:hideShow(this);', Resources::_('SEARCH'))
				);
			}
			$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);
			//get breadscrumb
			$breadscrumbs = array(
			new Sss_CommonEntity_BreadScrumb($this->_menuName, '')
			);
			$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
		}else{
			$this->_redirect("/$handle/inventory/rma");
		}
		//old
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$this->view->assign(Sss_Ultility_CommonViewParamater::HANDLE,$handle);
	}

	public function addAlertAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$alertTable              = new Shepherd_Model_Mapper_Alert();
		$shipmentTable           = new Shepherd_Model_Mapper_Shipment();
		$itemTable               = new Shepherd_Model_Mapper_Item();
		$hardwareTable           = new Application_Model_Mapper_Hardware();
		$containerTable          = new Shepherd_Model_Mapper_Container();

		$systemtypeShepherdTable = new Shepherd_Model_Mapper_SystemType();

		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;
		//get compare value
		$compareValue = $systemtypeShepherdTable->getSystemBySystemName(Sss_Ultility_SystemName::COMPARE_VALUE);
		//get params edit
		$params     = $this->getRequest()->getParams();
		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		$itemId     = $this->getRequest()->getParam('itemid', -1);

		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
		$shipmentId = $params['shipmentid'];

		if (($itemId == -1 || (int) $itemId == 0) && isset($params['itemid']))
		$itemId = $params['itemid'];

		$inventory = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::INVENTORY, -1);
		if (($inventory == -1 || (int) $inventory == 0) && isset($params[Sss_Ultility_CommonViewParamater::INVENTORY]))
		$inventory = $params[Sss_Ultility_CommonViewParamater::INVENTORY];
		$this->view->assign(Sss_Ultility_CommonViewParamater::INVENTORY, $inventory);

		$shipmentdetail = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL, -1);
		if (($shipmentdetail == -1 || (int) $shipmentdetail == 0) && isset($params[Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL]))
		$shipmentdetail = $params[Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL];
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL, $shipmentdetail);

		$shipment         = $shipmentTable->getShipmentById($shipmentId);
		$item             = $itemTable->getItemById($itemId);

		//get param
		$sensorid     = $this->getRequest()->getParam('hardware', -1);
		$graphvalueid = $this->getRequest()->getParam('alerttype', -1);
		$operatorid   = $this->getRequest()->getParam('operator', -1);

		//get dropdownlist graph value
		$graphValue = null;
		if($sensorid != -1 && $sensorid != null){
			$graphValue = $hardwareTable->getHardwareValueTypeList($sensorid);
		}

		//get name when page load
		$alertName = $this->getValueRequest('alertname', $this->getRequest()->getParam('alertname', Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
		$this->view->assign('alertname', $alertName);
		$alertValue = $this->getValueRequest('value', $this->getRequest()->getParam('value', Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
		$this->view->assign('value', $alertValue);
		$alertEmail = $this->getValueRequest('email', $this->getRequest()->getParam('email', Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
		$this->view->assign('email', $alertEmail);
		$alertNote = $this->getValueRequest('note', $this->getRequest()->getParam('note', Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
		$this->view->assign('note', $alertNote);

		$destination   = 0;
		$floorplanid   = 0;
		$strAction     = '';
		$strController = Sss_Ultility_CommonViewParamater::SHIPMENTCONTROLLER;
		$titlename     = '';
		$systemName    = '';
		$queryString = '';
		$floorplans    = $containerTable->getContainer($floorplanid);
		$floorplanName = $floorplans->containername;

		if ($shipment) {
			$floorplanName = $shipment->shipmentname;
			$destination   = $shipmentId;
			$strAction     = 'shipments';
			$titlename     = Resources::_('SHIPMENT_NAME');
			$sensors       = $hardwareTable->getSensorList($shipmentId, Sss_Ultility_SystemName::SHIPMENTS);
			$systemName    = Sss_Ultility_SystemName::SHIPMENTS;
			$queryString = '?shipmentid=' . $shipmentId ;
		}
		if ($item) {
			$floorplanName = $item->itemname;
			$destination   = $itemId;
			$strAction     = 'shipments';
			$titlename     = Resources::_('ITEM_NAME');
			$sensors       = $hardwareTable->getSensorList($itemId, Sss_Ultility_SystemName::ITEMS);
			$systemName    = Sss_Ultility_SystemName::ITEMS;
			$queryString   = '?itemid=' . $itemId;
		}
		if ($inventory != -1) {
			$strController = Sss_Ultility_CommonViewParamater::SHIPMENTCONTROLLER;
			$strAction     = 'alert';
			if ($shipmentdetail != -1) {
				$strAction = Sss_Ultility_CommonViewParamater::ACTION_SHIPMENT_DETAIL;
			}
			$queryString = '?shipmentid=' . $shipmentId . '&' . Sss_Ultility_CommonViewParamater::INVENTORY . '=' . $inventory;
		}

		$this->view->assign(Sss_Ultility_CommonViewParamater::QUERY_STRING, $queryString);
		$this->view->assign(Sss_Ultility_CommonViewParamater::CONTROLLER, $strController);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION, $strAction);
		$this->view->assign('containername', $floorplanName);
		$this->view->assign(Sss_Ultility_CommonViewParamater::TITLE_NAME, $titlename);

		//display to view
		$this->view->assign('itemid', $itemId);
		$this->view->assign('shipmentid', $shipmentId);
		$this->view->assign('settingid', $settingId);
		$this->view->assign('hardware', $sensorid);
		$this->view->assign('alerttype', $graphvalueid);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SENSORS, $sensors);
		$this->view->assign(Sss_Ultility_CommonViewParamater::GRAPH_VALUE, $graphValue);
		$this->view->assign(Sss_Ultility_CommonViewParamater::COMPARE_VALUE, $compareValue);
		$this->view->assign('operator', $operatorid);

		if (isset($params[Sss_Ultility_CommonViewParamater::BTN_SAVE])) {
			if ($userInformation->username) {
				$createdBy = $userInformation->accountid;
				$alertContructor = new Shepherd_Model_Alert();
				$alertContructor->setAlertName(addslashes($params['alertname']));
				$alertContructor->setHardware($params['hardware']);
				$alertContructor->setAlertType($params['alerttype']);
				$alertContructor->setOperator($params['operator']);
				$alertContructor->setValue($params['value']);
				$alertContructor->setEmail(addslashes($params['email']));
				$alertContructor->setNote(addslashes($params['note']));
				$alertContructor->setDestination($destination);
				$alertContructor->setIsused(1);
				$alertContructor->setCreatedDate(Sss_Ultility_Common::getDateTimeNumber());
				$alertContructor->setCreatedBy($createdBy);
				$alertContructor->setUpdatedBy($createdBy);
				$alertContructor->setUpdatedDate(Sss_Ultility_Common::getDateTimeNumber());
				$alertContructor->setSystemName($systemName);
				$alertId   = $alertTable->save($alertContructor,$instanceId);
				$this->getValueRequest('alertname', '', Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
				$this->getValueRequest('value', '', Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
				$this->getValueRequest('email', '', Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
				$this->getValueRequest('note', '', Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
				if (isset($alertId)) {
					$this->redirect(Resources::_('ADD_ALERT_SUCCESSFULL'), $this->view->url(array(
					Sss_Ultility_CommonViewParamater::CONTROLLER => $strController,
					Sss_Ultility_CommonViewParamater::ACTION => $strAction
					)) . $queryString);
				} else {
					$this->redirect(Resources::_('ADD_ALERT_FAILED'), $this->view->url(array(
					Sss_Ultility_CommonViewParamater::CONTROLLER => $strController,
					Sss_Ultility_CommonViewParamater::ACTION => $strAction
					)) . $queryString);
				}
			} else {
				$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
				$this->_redirect("/$handle/user/login");
			}
		}
		//get breadscrumb
		$breadscrumbs = null;
		if ($inventory != -1) {
			$breadscrumbs = array(
			new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
			new Sss_CommonEntity_BreadScrumb(Resources::_('ALERTS'), '#'),
			new Sss_CommonEntity_BreadScrumb(Resources::_('ADD_ALERT'), '#')
			);
		} else {
			$breadscrumbs = array(
			new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
			new Sss_CommonEntity_BreadScrumb(Resources::_('ADD_ALERT'), '#')
			);
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}

	public function editAlertAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$alertTable              = new Shepherd_Model_Mapper_Alert();
		$shipmentTable           = new Shepherd_Model_Mapper_Shipment();
		$itemTable               = new Shepherd_Model_Mapper_Item();
		$hardwareTable           = new Application_Model_Mapper_Hardware();
		$containerTable          = new Shepherd_Model_Mapper_Container();

		$systemtypeShepherdTable = new Shepherd_Model_Mapper_SystemType();
		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;

		//get compare value
		$compareValue = $systemtypeShepherdTable->getSystemBySystemName(Sss_Ultility_SystemName::COMPARE_VALUE);

		//get params edit
		$params     = $this->getRequest()->getPost();
		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		$alertId    = $this->getRequest()->getParam('alertid', -1);
		$itemId     = $this->getRequest()->getParam('itemid', -1);
		if (($alertId == -1 || (int) $alertId == 0) && isset($params['alertid']))
		$alertId = $params['alertid'];
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
		$shipmentId = $params['shipmentid'];
		if (($itemId == -1 || (int) $itemId == 0) && isset($params['itemid']))
		$itemId = $params['itemid'];
		$inventory = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::INVENTORY, -1);
		if (($inventory == -1 || (int) $inventory == 0) && isset($params[Sss_Ultility_CommonViewParamater::INVENTORY]))
		$inventory = $params[Sss_Ultility_CommonViewParamater::INVENTORY];
		$this->view->assign(Sss_Ultility_CommonViewParamater::INVENTORY, $inventory);

		$shipment         = $shipmentTable->getShipmentById($shipmentId);
		$alert            = $alertTable->getAlertbyId($alertId);
		$item             = $itemTable->getItemById($itemId);
		//get param
		$sensorid     = $this->getRequest()->getParam('hardware', -1);
		$graphvalueid = $this->getRequest()->getParam('alerttype', -1);
		$operatorid   = $this->getRequest()->getParam('operator', -1);
		if($sensorid == -1){
			$sensorid = $alert->hardware;
		}
		//get dropdownlist graph value
		$graphValue = null;
		if($sensorid != -1 && $sensorid != null){
			$graphValue = $hardwareTable->getHardwareValueTypeList($sensorid);
		}
		$destination   = 0;
		$floorplanid   = 0;
		$strAction     = '';
		$strController = Sss_Ultility_CommonViewParamater::SHIPMENTCONTROLLER;
		$alertName     = '';
		$value         = '';
		$email         = '';
		$note          = '';
		$titlename     = '';
		$sensors       = $hardwareTable->getSensorList("");
		if ($alert) {
			if ($sensorid == -1)
			$sensorid = $alert->hardware;
			if ($graphvalueid == -1)
			$graphvalueid = $alert->alerttype;
			if ($operatorid == -1)
			$operatorid = $alert->operator;
			//get name when page load
			$alertName = $this->getValueRequest('alertname', $this->getRequest()->getParam('alertname', Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
			$this->view->assign('alertname', $alertName);
			$value = $this->getValueRequest('value', $this->getRequest()->getParam('value', Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
			$this->view->assign('value', $value);
			$email = $this->getValueRequest('email', $this->getRequest()->getParam('email', Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
			$this->view->assign('email', $email);
			$note = $this->getValueRequest('note', $this->getRequest()->getParam('note', Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
			$this->view->assign('note', $note);
			if($alertName == ''){
				$alertName = $alert->alertname;
			}
			if($value == ''){
				$value     = $alert->value;
			}
			if($email == ''){
				$email     = $alert->email;
			}
			if($note == ''){
				$note      = $alert->note;
			}
			if (!$titlename)
			$titlename = Resources::_('ITEM_NAME');
			if (!$item)
			$titlename = Resources::_('SHIPMENT_NAME');

			$titlename   = Resources::_('FLOORPLAN');
		}
		$floorplans    = $containerTable->getContainer($floorplanid);
		$floorplanName = $floorplans->containername;
		if ($shipment) {
			$floorplanName = $shipment->shipmentname;
			$destination   = $shipmentId;
			if (!$strAction)
			$strAction = 'shipments';
			$sensors = $hardwareTable->getSensorList($shipmentId, Sss_Ultility_SystemName::SHIPMENTS);
		}
		if ($item) {
			$floorplanName = $item->itemname;
			$destination   = $itemId;
			if (!$strAction)
			$strAction = 'shipments';
			$sensors = $hardwareTable->getSensorList($itemId, Sss_Ultility_SystemName::ITEMS);
		}
		$queryString = '';
		if ($inventory != -1 && $inventory != '') {
			$strController = Sss_Ultility_CommonViewParamater::SHIPMENT;
			$strAction     = 'alert';
			$queryString   = '?shipmentid=' . $shipmentId . '&' . Sss_Ultility_CommonViewParamater::INVENTORY . '=' . $inventory;
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::QUERY_STRING, $queryString);
		$this->view->assign(Sss_Ultility_CommonViewParamater::CONTROLLER, $strController);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION, $strAction);
		$this->view->assign('containername', $floorplanName);
		$this->view->assign(Sss_Ultility_CommonViewParamater::TITLE_NAME, $titlename);

		//display to view
		$this->view->assign('itemid', $itemId);
		$this->view->assign('shipmentid', $shipmentId);
		$this->view->assign('hardware', $sensorid);
		$this->view->assign('alerttype', $graphvalueid);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SENSORS, $sensors);
		$this->view->assign(Sss_Ultility_CommonViewParamater::GRAPH_VALUE, $graphValue);
		$this->view->assign('alertname', $alertName);
		$this->view->assign('value', $value);
		$this->view->assign('email', $email);
		$this->view->assign('note', $note);
		$this->view->assign(Sss_Ultility_CommonViewParamater::COMPARE_VALUE, $compareValue);
		$this->view->assign('operator', $operatorid);
		$this->view->assign('alertid', $alertId);
		if (isset($params[Sss_Ultility_CommonViewParamater::BTN_SAVE])) {
			if ($userInformation->username) {
				$updateBy = $userInformation->accountid;
				$alertUpdate = $alertTable->findById($alertId,$instanceId);
				$alertUpdate->setAlertName(addslashes($params['alertname']));
				$alertUpdate->setHardware($params['hardware']);
				$alertUpdate->setAlertType($params['alerttype']);
				$alertUpdate->setOperator($params['operator']);
				$alertUpdate->setValue($params['value']);
				$alertUpdate->setEmail(addslashes($params['email']));
				$alertUpdate->setNote(addslashes($params['note']));
				$alertUpdate->setDestination($destination);
				$alertUpdate->setIsused(1);
				$alertUpdate->setUpdatedDate(Sss_Ultility_Common::getDateTimeNumber());
				$alertUpdate->setUpdatedBy($updateBy);
				$updateId = $alertTable->save($alertUpdate,$instanceId);
				if (isset($updateId)) {
					$this->redirect(Resources::_('EDIT_ALERT_SUCCESSFULL'), $this->view->url(array(
					Sss_Ultility_CommonViewParamater::CONTROLLER => $strController,
					Sss_Ultility_CommonViewParamater::ACTION => $strAction
					)) . $queryString);
				} else {
					$this->redirect(Resources::_('EDIT_ALERT_FAILED'), $this->view->url(array(
					Sss_Ultility_CommonViewParamater::CONTROLLER => $strController,
					Sss_Ultility_CommonViewParamater::ACTION => $strAction
					)) . $queryString);
				}
			} else {
				$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
				$this->_redirect("/$handle/user/login");
			}
		}

		//get breadscrumb
		$breadscrumbs = null;
		if ($inventory != -1) {
			$breadscrumbs = array(
			new Sss_CommonEntity_BreadScrumb($this->_menuNameInventory, ''),
			new Sss_CommonEntity_BreadScrumb(Resources::_('SHIPMENT'), '#'),
			new Sss_CommonEntity_BreadScrumb(Resources::_('ALERTS'), '#'),
			new Sss_CommonEntity_BreadScrumb(Resources::_('EDIT_ALERT'), '#')
			);
		} else {
			$breadscrumbs = array(
			new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
			new Sss_CommonEntity_BreadScrumb(Resources::_('CONFIGURATION'), '#'),
			new Sss_CommonEntity_BreadScrumb(Resources::_('EDIT_ALERT'), '#')
			);
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}

	public function shipmentHistoryAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$this->getValueRequest('shipmentname', '', Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);


		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::VISIBLE, 'feature', 'action' ,$this->listRole(), true) == 1 ){
			$shipmentTable = new Shepherd_Model_Mapper_Shipment();
			$alertTable    = new Shepherd_Model_Mapper_Alert();
			$productTable  = new Shepherd_Model_Mapper_Product();
			$documentTable = new Shepherd_Model_Mapper_Document();
			$hardwareTable = new Application_Model_Mapper_Hardware();


			$stockLocationTable = new Application_Model_Mapper_StockLocation();

			//get order, page, limit param
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD] = $this->getValueRequest(Sss_Ultility_CommonViewParamater::ORDERFIELD, $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::ORDERFIELD, Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE]  = $this->getValueRequest(Sss_Ultility_CommonViewParamater::ORDERTYPE, $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::ORDERTYPE, Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);
			$filterParam[Sss_Ultility_CommonViewParamater::PAGE]       = $this->getValueRequest(Sss_Ultility_CommonViewParamater::PAGE, $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::PAGE, Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);

			$limit = $this->getValueRequest(Sss_Ultility_CommonViewParamater::LIMIT, $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::LIMIT, Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);

			//test param filter: omit title text in field
			$paramOriginal        = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'origin', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramDestination     = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'destination', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramFromDate        = $this->getRequest()->getParam('date_from', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramToDate          = $this->getRequest()->getParam('date_to', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramInternalRef     = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'internal_ref', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramSupplierRef     = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'supplier_ref', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramReferenceNumber = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'reference_number', Sss_Ultility_CommonViewParamater::UNDERFINE);



			//get shipment filter
			$filterParam['origin']            = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'origin', trim($paramOriginal), Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);
			$filterParam['destination']      = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'destination', trim($paramDestination), Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);
			$filterParam['date_from']        = $this->getValueRequest('date_from', $paramFromDate, Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);
			$filterParam['date_to']          = $this->getValueRequest('date_to', $paramToDate, Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);
			$filterParam['internal_ref']     = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'internal_ref', trim($paramInternalRef), Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);
			$filterParam['supplier_ref']     = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'supplier_ref', $paramSupplierRef, Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);
			$filterParam['reference_number'] = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'reference_number', trim($paramReferenceNumber), Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);

			$limit = $this->getValueRequest(Sss_Ultility_CommonViewParamater::LIMIT, $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::LIMIT, Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::SHIPMENT_HISTORY_SESSION);

			$params     = $this->getRequest()->getPost();

			if ($userInformation->username) {
				//update hardware when detach
				if(isset($params['hardwareid']) && isset($params[Sss_Ultility_CommonViewParamater::BTN_SAVE])){
					$hardwareIds = $params['hardwareid'];
					$stockLocationIds =  $params['stocklocationid'];
					$arrHardwareIds = explode(',', $hardwareIds);
					$arrStockLocationIds = explode(',', $stockLocationIds);
					$id;
					try{
						for ($i = 0; $i < count($arrHardwareIds); $i++) {
							$hardwareUpdate = $hardwareTable->findById($arrHardwareIds[$i]);
							$hardwareUpdate->setDetachLocationName($params['detach_location_name'.$arrHardwareIds[$i]]);
							$hardwareUpdate->setStatus(Sss_Ultility_Setting::HARDWARE_STATUS_DETACH);
							$hardwareUpdate->setDetachLocationId(null);
							if($arrStockLocationIds[$i] != '' && (int)$arrStockLocationIds[$i])
							{
								$hardwareUpdate->setDetachLocationId($arrStockLocationIds[$i]);
								$hardwareUpdate->setStatus(Sss_Ultility_Setting::HARDWARE_STATUS_DETACH);
							}
							$id   = $hardwareTable->save($hardwareUpdate);
						}
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DETACH_SENSOR_SUCCESSFULL'));
					} catch (exception $e) {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DETACH_SENSOR_FAIL'));
					}
				}
			}
			$filterParam['shipment_history'] = 1;

			//get shipment list
			$list                 = $shipmentTable->getList($limit, 0, $filterParam);
			$row                  = $list->list;
			$listAlertShipment    = array();
			$listProductShipment  = array();
			$listDocumentShipment = array();
			$listSensorShipment   = array();
			foreach ((array) $row as $r) {
				//get alert list for shipment
				$listAlert           = $alertTable->getList(Sss_Ultility_SystemName::SHIPMENTS, $r['shipmentid']);
				$listAlertShipment[] = count($listAlert);

				//get product list for shipment
				$listProduct           = $productTable->getList($r['shipmentid']);
				$listProductShipment[] = count($listProduct);

				//get document list for shipment
				$listDocument           = $documentTable->getList($r['shipmentid']);
				$listDocumentShipment[] = count($listDocument);

				//get sensor list for shipment
				$listSensor           = $hardwareTable->getListByDestination($r['shipmentid'], Sss_Ultility_SystemName::SHIPMENTS);
				$listSensorShipment[] = $listSensor;

			}
			//get stock location 
			$stockLocation = $stockLocationTable->getAllStockLocation();

			$ArrStockLocation = array();
			$dataAuto = '';
			foreach($stockLocation as $sl)
			{
				$ArrStockLocation[$sl['stocklocationname']]= $sl['stocklocationid'];
			}
			foreach ($ArrStockLocation as $key=>$value) {
				$dataAuto = $dataAuto."$key|$value#";
			}
			$dataAuto = substr($dataAuto, 0 ,strlen($dataAuto) - 1);
			$this->view->assign(Sss_Ultility_CommonViewParamater::AUTOCOMPLETE, $dataAuto);

			$countSensor = 0;
			foreach($listSensorShipment as $ls)
			{
				foreach($ls as $s)
				{
					$countSensor = $countSensor + 1;
				}
			}

			//get sensor list by shipment
			$hardwareByShipment = $hardwareTable->getHardwareByDestinationGraph('', Sss_Ultility_SystemName::SHIPMENTS, 0);
			$chartsByShipment = array();
			$index = 0;

			foreach ($hardwareByShipment as $tmp) {
				$chartIds = '';  
				$chartList =    $hardwareTable->getChartIds($tmp['hardwareid'], $tmp['systemtypeid']);
				if($chartList){
					foreach($chartList as $c){
						if($chartIds == ''){
							$chartIds = $c['chart'];
						}else{
							$chartIds = $chartIds . ',' .  $c['chart'];
						}
					}
				}
				$tmp['chart'] = $chartIds; 
				$chartsByShipment[$index]     = $tmp;
				$index = $index + 1;
			}
			$this->view->assign(Sss_Ultility_CommonViewParamater::HARDWARE_BY_SHIPMENT, $chartsByShipment);

			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM_COUNT, $countSensor);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_ALERT_SHIPMENT, $listAlertShipment);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_PRODUCT_ITEM, $listProductShipment);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_DOCUMENT_ITEM, $listDocumentShipment);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM, $listSensorShipment);

			$pagination = $list->pagination;
			$this->view->assign(Sss_Ultility_CommonViewParamater::FILTER_PARAM, $filterParam);
			$this->view->assign(Sss_Ultility_CommonViewParamater::LIMIT, $limit);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $row);
			$this->view->assign(Sss_Ultility_CommonViewParamater::PAGINATION, $pagination);


			$actionbuttons = array(
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::EXPORT, 'shipment-history-export?shipmenthistory=1&export=1', Resources::_('EXPORT')),
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::PRINTF, 'shipment-history-export?shipmenthistory=1&print=1', Resources::_('PRINT')),
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::SEARCH, 'javascript:hideShow(this);', Resources::_('SEARCH'))
			);
			//check permission
			if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::ADD, 'feature', 'action' ,$this->listRole(), true) != 1 )
			{
				$actionbuttons = array(
				new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::EXPORT, 'shipment-history-export?export=1', Resources::_('EXPORT')),
				new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::PRINTF, 'shipment-history-export?print=1', Resources::_('PRINT')),
				new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::SEARCH, 'javascript:hideShow(this);', Resources::_('SEARCH'))
				);
			}
			$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);

			//get breadscrumb
			$breadscrumbs = array(
			new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
			new Sss_CommonEntity_BreadScrumb(Resources::_('SHIPMENT_HISTORY'), '#')
			);
			$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
		}
	}
	
	public function shipmentDetailAction()
	{
		$systemTypeTable = new Shepherd_Model_Mapper_SystemType();
		$shipmentTable   = new Shepherd_Model_Mapper_Shipment();
		$alertTable      = new Shepherd_Model_Mapper_Alert();
		$productTable    = new Shepherd_Model_Mapper_Product();
		$documentTable   = new Shepherd_Model_Mapper_Document();
		$hardwareTable   = new Application_Model_Mapper_Hardware();
		$allocationTable = new Shepherd_Model_Mapper_Allocation();
		$stockLocationTable = new Application_Model_Mapper_StockLocation();
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;

		//get shipment type
		$shipmenttypeId = $this->getRequest()->getParam('systemtypeid', -1);

		$this->view->assign('shipmenttype', $shipmenttypeId);

		$shipmentTypes = $systemTypeTable->getSystemBySystemName(Sss_Ultility_SystemName::SHIPMENT_TYPE);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_TYPES, $shipmentTypes);

		//get shipmentid
		$params     = $this->getRequest()->getPost();

		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
		$shipmentId = $params['shipmentid'];
		$this->view->assign('shipmentid', $shipmentId);

		//get shipment by id
		$shipment = $shipmentTable->getShipmentById($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT, $shipment);

		//get stock location 
		$stockLocation = $stockLocationTable->getAllStockLocation();

		$ArrStockLocation = array();
		$dataAuto = '';
		foreach($stockLocation as $sl) {
			$ArrStockLocation[$sl['stocklocationname']]= $sl['stocklocationid'];
		}
		foreach ($ArrStockLocation as $key=>$value) {
			$dataAuto = $dataAuto."$key|$value#";
		}
		$dataAuto = substr($dataAuto, 0 ,strlen($dataAuto) - 1);
		$this->view->assign(Sss_Ultility_CommonViewParamater::AUTOCOMPLETE, $dataAuto);

		//list alert for shipment detail
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		if ($userInformation->username) {
			//delete alert
			$deleteAlertId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_ALERT_ID, -1);
			if (isset($deleteAlertId) && $deleteAlertId != -1) {
				$deleteId   = $alertTable->deleteRow($deleteAlertId,$instanceId);
				if (isset($deleteId)) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_ALERT_SUCCESSFULL'));
				} else {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_ALERT_FAIL'));
				}
			}
			//update hardware when detach
			if(isset($params['hardwareid']) && isset($params[Sss_Ultility_CommonViewParamater::BTN_SAVE])){ 
				$hardwareId = $params['hardwareid'];

				$date = new DateTime();
				$enddt = $date->format(DATETIME_MYSQL_FORMAT);
				$getAllocationId = $allocationTable->getById($shipmentId,Sss_Ultility_SystemName::SHIPMENTS,$instanceId,$hardwareId);
				if($getAllocationId->allocationid != null) {
					$allocationUpdate = $allocationTable->findById($getAllocationId->allocationid,$instanceId);
					$allocationUpdate->setStatus(0);
					$allocationUpdate->setNote($params['detach_location_name'.$hardwareId]);
					$allocationUpdate->setEnddt($enddt);
					$allocationTable->save($allocationUpdate,$instanceId);
				}
				$hardwareUpdate = $hardwareTable->findById($hardwareId);
				$hardwareUpdate->setDetachLocationName($params['detach_location_name'.$hardwareId]);
				if((int)$params['stocklocationid'])
				{
					$hardwareUpdate->setDetachLocationId($params['stocklocationid']);
				}
				$id   = $hardwareTable->save($hardwareUpdate);

				if (isset($id)) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DETACH_SENSOR_SUCCESSFULL'));
				} else {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DETACH_SENSOR_FAIL'));
				}
			}
		}

		//get alert list for shipment
		$listAlertShipment = $alertTable->getList(Sss_Ultility_SystemName::SHIPMENTS, $shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_ALERT_SHIPMENT, $listAlertShipment);

		//list product for shipment detail
		if ($userInformation->username) {
			//delete product
			$deleteProductId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_PRODUCT_ID, -1);
			if (isset($deleteProductId) && $deleteProductId != -1) {
				$deleteId     = $productTable->deleteRow($deleteProductId,$instanceId);
				if (isset($deleteId)) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_PRODUCT_SUCCESSFULL'));
				} else {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_PRODUCT_FAIL'));
				}
			}
		}

		//get product list for shipment
		$listProductShipment = $productTable->getList($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_PRODUCT_ITEM, $listProductShipment);

		//list document for shipment detail
		if ($userInformation->username) {
			//delete document
			$deleteDocumentId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_DOCUMENT_ID, -1);
			if (isset($deleteDocumentId) && $deleteDocumentId != -1) {
				$deleteId      = $documentTable->deleteRow($deleteDocumentId,$instanceId);
				if (isset($deleteId)) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_DOCUMENT_SUCCESSFULL'));
				} else {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_DOCUMENT_FAIL'));
				}
			}
		}

		//get document list for shipment
		$listDocumentShipment = $documentTable->getList($shipmentId,$instanceId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_DOCUMENT_ITEM, $listDocumentShipment);

		//list sensor for shipment detail
		if ($userInformation->username) {
			//remove sensor from shipment
			$deleteSensorId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_SENSOR_ID, -1);
			if (isset($deleteSensorId) && $deleteSensorId != -1) {
				$arrId    = array(
				'hardware = ?' => (int) $deleteSensorId,
				'destination = ?' => (int) $shipmentId,
				'systemname = ?' => Sss_Ultility_SystemName::SHIPMENTS
				);
				$deleteId = $allocationTable->deleteRow(null,$arrId,$instanceId);
				if (isset($deleteId)) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_HARDWARE_SUCCESSFULL'));
				} else {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_HARDWARE_FAIL'));
				}
			}
		}

		//get sensor list for shipment
		$listSensorShipment = $hardwareTable->getListByDestination($shipmentId, Sss_Ultility_SystemName::SHIPMENTS);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM, $listSensorShipment);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM_COUNT, count($listSensorShipment));

		//action button
		$actionbuttons = array(
		new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::PRINTF, 'shipment-detail-export?print=1&' . "shipmentid=$shipmentId", Resources::_('PRINT')),
		new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::BACK, 'shipments', Resources::_('BTN_BACK'))
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);

		//get breadscrumb
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb(Resources::_('SHIPMENT_DETAIL'), '#')
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}

	public function alertAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());


		$alertTable = new Shepherd_Model_Mapper_Alert();

		//get shipmentid
		$params     = $this->getRequest()->getPost();
		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
			$shipmentId = $params['shipmentid'];
		$this->view->assign('shipmentid', $shipmentId);

		$this->getValueRequest('alertname', '', Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
		$this->getValueRequest('value', '', Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
		$this->getValueRequest('email', '', Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);
		$this->getValueRequest('note', '', Sss_Ultility_SessionName::ADD_NEW_ALERT_SESSION);

		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		if ($userInformation->username) {
			//delete alert
			$deleteAlertId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_ALERT_ID, -1);
			if (isset($deleteAlertId) && $deleteAlertId != -1) {
				$deleteId   = $alertTable->deleteRow($deleteAlertId);
				if (isset($deleteId)) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_ALERT_SUCCESSFULL'));
				} else {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_ALERT_FAIL'));
				}
			}
		}

		//get alert list for shipment
		$listAlertShipment = $alertTable->getList(Sss_Ultility_SystemName::SHIPMENTS, $shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_ALERT_SHIPMENT, $listAlertShipment);

		$this->view->assign(Sss_Ultility_CommonViewParamater::OTHER_QUERY_STRING, '?' . 'shipmentid' . "=" . $shipmentId . '&' . Sss_Ultility_CommonViewParamater::INVENTORY . '=1');
		$this->view->assign(Sss_Ultility_CommonViewParamater::OTHER_CONTROLLER_NAME, Sss_Ultility_CommonViewParamater::SHIPMENTCONTROLLER);

		//action button
		$actionbuttons = array(
		new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::ADDOTHER, "add-alert", Resources::_('ADD_ALERT')),
		new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::BACK, 'shipments', Resources::_('BTN_BACK'))
		);

		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::ADD, 'feature', 'action' ,$this->listRole(), true) != 1 )
		{
			$actionbuttons = array(
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::BACK, 'shipments', Resources::_('BTN_BACK'))
			);
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);

		//get breadscrumb        
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb(Resources::_('ALERTS'), '#')
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}

	public function productAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;
		$productTable = new Shepherd_Model_Mapper_Product();
		//get shipmentid
		$params     = $this->getRequest()->getPost();
		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
			$shipmentId = $params['shipmentid'];
		$this->view->assign('shipmentid', $shipmentId);

		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		if ($userInformation->username) {
			//delete product
			$deleteProductId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_PRODUCT_ID, -1);
			if (isset($deleteProductId) && $deleteProductId != -1) {
				$deleteId     = $productTable->deleteRow($deleteProductId,$instanceId);
				if (isset($deleteId)) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_PRODUCT_SUCCESSFULL'));
				} else {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_PRODUCT_FAIL'));
				}
			}
		}

		//get product list for shipment
		$listProductShipment = $productTable->getList($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_PRODUCT_ITEM, $listProductShipment);
		$this->view->assign(Sss_Ultility_CommonViewParamater::OTHER_QUERY_STRING, '?shipmentid=' . $shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::OTHER_CONTROLLER_NAME, Sss_Ultility_CommonViewParamater::SHIPMENTCONTROLLER);
		//action button
		$actionbuttons = array(
		new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::ADDOTHER, "add-product", Resources::_('ADD_DOCUMENT')),
		new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::BACK, 'shipments', Resources::_('BTN_BACK'))
		);

		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::ADD, 'feature', 'action' ,$this->listRole(), true) != 1 ) {
			$actionbuttons = array(
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::BACK, 'shipments', Resources::_('BTN_BACK'))
			);
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);

		//get breadscrumb
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb(Resources::_('PRODUCT'), '#')
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}

	public function shipmentHardwareAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::EDIT, 'feature', 'action' ,$this->listRole(), true) != 1 ) {
			$this->_redirect("/$handle/user/login");
		}
		$hardwareTable   = new Application_Model_Mapper_Hardware();
		$allocationTable = new Shepherd_Model_Mapper_Allocation();
		$shipmentTable   = new Shepherd_Model_Mapper_Shipment();
		$inventoryTable  = new Application_Model_Mapper_Inventory();
		$stocklocationTable  = new Application_Model_Mapper_StockLocation();
		$stockLocationInstanceTable = new Application_Model_Mapper_StockLocationInstance();
		$instanceTable  = new Application_Model_Mapper_Instance(); 
		$params     = $this->getRequest()->getParams();
		$isSearch = isset($params['s']) && $params['s'] == 1;
		//get shipment type
		$status = $this->getRequest()->getParam('status', -1);
		if (($status == -1 || (int) $status == 0) && isset($params['status']))
			$status = $params['status'];
		$this->view->assign('status', $status);

		//get order, page, limit param
		$filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD] = isset($params[Sss_Ultility_CommonViewParamater::ORDERFIELD]) ? $params[Sss_Ultility_CommonViewParamater::ORDERFIELD] : null;
		$filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE]  = isset($params[Sss_Ultility_CommonViewParamater::ORDERTYPE]) ? $params[Sss_Ultility_CommonViewParamater::ORDERTYPE] : null;
		$filterParam[Sss_Ultility_CommonViewParamater::PAGE] = isset($params[Sss_Ultility_CommonViewParamater::PAGE]) ? $params[Sss_Ultility_CommonViewParamater::PAGE] : 1;
		//test param filter: omit title text in field
		$paramHardwareName   = $isSearch? strcmp($this->getRequest()->getParam('hardwarename', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('HARDWARE_NAME')) == 0 ? '' : $this->getRequest()->getParam('hardwarename', Sss_Ultility_CommonViewParamater::UNDERFINE):'';
		$paramHardwareSerial = $isSearch?strcmp($this->getRequest()->getParam('serial_number', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('SERIAL')) == 0 ? '' : $this->getRequest()->getParam('serial_number', Sss_Ultility_CommonViewParamater::UNDERFINE):'';
		$paramHardwareVendor = $isSearch?strcmp($this->getRequest()->getParam('vendor', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('VENDOR')) == 0 ? '' : $this->getRequest()->getParam('vendor', Sss_Ultility_CommonViewParamater::UNDERFINE):'';
		//get monitor id
		$monitorId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::MONITORING_LOCALIZATION, -1);
		$this->view->assign(Sss_Ultility_CommonViewParamater::MONITOR, $monitorId);
		//get harware filter
		$filterParam['hardwarename'] = $this->getValueRequest('hardwarename', trim($paramHardwareName), Sss_Ultility_SessionName::SHIPMENT_HARDWARE_SESSION);
		$filterParam['serial_number'] = $this->getValueRequest('serial_number', trim($paramHardwareSerial), Sss_Ultility_SessionName::SHIPMENT_HARDWARE_SESSION);
		$filterParam['vendor']        = $this->getValueRequest('vendor', trim($paramHardwareVendor), Sss_Ultility_SessionName::SHIPMENT_HARDWARE_SESSION);

		$defaultNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::SHEPHERD_SYSTEM . $this->getCurrentHandle());
		$filterParam['stocklocation'] = $defaultNamespace->stocklocationId;

		//get shipmentid
		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
			$shipmentId = $params['shipmentid'];
		$this->view->assign('shipmentid', $shipmentId);

		$shipmentdetail = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL, -1);
		if (($shipmentdetail == -1 || (int) $shipmentdetail == 0) && isset($params[Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL]))
			$shipmentdetail = $params[Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL];
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL, $shipmentdetail);
		$instance = $instanceTable->getInstanceByHandle($handle);
		if($instance) {
			$stockLocations = $stockLocationInstanceTable->getStockLocationByInstance($instance->instanceid);
		}
		$stockLocationIds = '';
		if(count($stockLocations) > 0){ 
			foreach($stockLocations as $s){
				$stockLocationIds = $stockLocationIds == '' ? $s['stocklocation'] : $stockLocationIds.','.$s['stocklocation'];
			}
		}
		if($stockLocationIds != ''){
			$filterParam['stocklocation'] = $stockLocationIds;
		}
		//get shipment
		$shipment     = $shipmentTable->getShipmentById($shipmentId);
		$shipmentName = $shipment->shipmentname;
		$shipmentType = $shipment->shipmenttype;
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_NAME, $shipmentName);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_TYPE, $shipmentType);
		if ($params && $userInformation->username && !isset($params["btnSearch"])  && isset($params["ids"])) {
			//assign hardware to item
			$arrAssignHardwareId = explode('_', $params[Sss_Ultility_CommonViewParamater::IDS]);
			if (false != $arrAssignHardwareId) {
				try {
					for ($i = 0; $i < count($arrAssignHardwareId); $i++) {
						$valueHardware = $arrAssignHardwareId[$i];
						if ($valueHardware > 0 && $valueHardware != '') {

							$allocationContructor = new Shepherd_Model_Allocation();
							$allocationContructor->setHardware($valueHardware);
							$allocationContructor->setDestination($shipmentId);
							$allocationContructor->setStatus(1);
							$allocationContructor->setSystemName(Sss_Ultility_SystemName::SHIPMENTS);
							$allocationContructor->setDescription($params['description']);
							$checkInsert = $allocationTable->checkAssignTo($valueHardware, $shipmentId, Sss_Ultility_SystemName::SHIPMENTS);
							if ($checkInsert == true) {
								$allocatinId = $allocationTable->save($allocationContructor,$instance->instanceid);
								//get hardware
								$hardware = $hardwareTable->getHardwareById($valueHardware);
								if($allocatinId){ 
									if($shipment->origin != 0 && $shipment->destination != 0){
										$origin =  $stocklocationTable->select($shipment->origin);
										$destination =  $stocklocationTable->select($shipment->destination);
										$allocationName = $hardware->stocklocationname.', '.$shipment->shipmentname.'( '.Resources::_('TITLE_FROM').' '.$origin->stocklocationname.' '.Resources::_('TITLE_TO').' '.$destination->stocklocationname.')';
									}else{
										$allocationName = $hardware->stocklocationname.', '.$shipment->shipmentname;
									}
									$hardwareUpdate = $hardwareTable->findById($valueHardware);
									$hardwareUpdate->setAllocation($allocationName);
									$hardwareTable->save($hardwareUpdate);
								}
								//process update hardware
								if ($shipment->shipmenttype == Sss_Ultility_Setting::RMA) {
									$hardwareUpdate = $hardwareTable->findById($valueHardware);
									$hardwareUpdate->setStatus(Sss_Ultility_Setting::OVIS_RMA);
									$Id            = $hardwareTable->save($hardwareUpdate);
									if (isset($Id)) {
										$arrInventoryId = array(
										'stocklocation' => (int) $hardware->stocklocationid,
										'isactivated' => true,
										'hardware' => $valueHardware
										);
										$getInventoryId = $inventoryTable->findIdByData($arrInventoryId);
										if(!empty($getInventoryId->inventoryid)) {
											$inventoryUpdate = $inventoryTable->findById($getInventoryId->inventoryid);
											$inventoryUpdate->setStatus(Sss_Ultility_Setting::OVIS_RMA);
											$inventoryTable->save($arrInventoryId, $data, false);
										}
									}
								}
							}
						}
					}
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('SENSOR_ATTACH_TO_SHIPMENT_SUCCESSFULL')); 
				} catch(exception $e) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('SENSOR_ATTACH_TO_SHIPMENT_FAIL'));
				}
			}
		}
		//get query string
		$strAction   = 'shipments';
		$queryString = "?shipmentid=" . $shipmentId;
		if($status != -1){
			$strAction   = 'rma'; 
			$queryString = "?shipmentid=" . $shipmentId.'&'.'status' . "=" . Sss_Ultility_Setting::RMA; 
		}
		if ($shipmentdetail != -1) {
			$strAction   = Sss_Ultility_CommonViewParamater::ACTION_SHIPMENT_DETAIL;
			$queryString = "?shipmentid=" . $shipmentId . '&' . Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL . '=1';
			if($status != -1) {
				$strAction   = 'rma';
				$queryString = "?shipmentid=" . $shipmentId . '&' . Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL . '=1'.'&'.'status' . "=" . Sss_Ultility_Setting::RMA;	
			}
		}
		if($monitorId == 1){
			$strAction = "dynamicmonitoring";
			$queryString = $queryString.'&'.Sss_Ultility_CommonViewParamater::MONITORING_LOCALIZATION . '=1';
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION, $strAction);
		$this->view->assign(Sss_Ultility_CommonViewParamater::QUERY_STRING, $queryString);
		//get sensor list for shipment
		$listSensorShipment = $hardwareTable->getListByDestination($shipmentId, Sss_Ultility_SystemName::SHIPMENTS );
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM, $listSensorShipment);
		//get allocation
		$allocation = $allocationTable->getById($shipmentId, Sss_Ultility_SystemName::SHIPMENTS);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ALLOCATION, $allocation);
		$list       = $hardwareTable->getListForItem($this->view->limit, 0, $filterParam);
		$row        = $list->list;
		$pagination = $list->pagination;
		$this->view->assign(Sss_Ultility_CommonViewParamater::FILTER_PARAM, $filterParam);
		$this->view->assign('isSearch', $isSearch);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $row);
		$this->view->assign(Sss_Ultility_CommonViewParamater::PAGINATION, $pagination);

		//action button
		$actionbuttons = array(
		new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::SEARCH, 'javascript:hideShow(this);', Resources::_('SEARCH'))
		);
		if (count($row) <= 0)
			$actionbuttons = null;
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);
		//get breadscrumb
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb(Resources::_('ATTACH_SENSORS'), '#')
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}

	public function documentAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;

		$documentTable = new Shepherd_Model_Mapper_Document();

		//get shipmentid
		$params     = $this->getRequest()->getPost();
		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
			$shipmentId = $params['shipmentid'];
		$this->view->assign('shipmentid', $shipmentId);

		//get shipment type
		$status = $this->getRequest()->getParam('status', -1);
		if (($status == -1 || (int) $status == 0) && isset($params['status']))
			$status = $params['status'];
		$this->view->assign('status', $status);

		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		if ($userInformation->username) {
			//delete document
			$deleteDocumentId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_DOCUMENT_ID, -1);
			if (isset($deleteDocumentId) && $deleteDocumentId != -1) {
				$document = $documentTable->getDocumentById($deleteDocumentId);
				if ($document->path) {
					if (file_exists(getcwd() . $document->path))
						unlink(getcwd() . $document->path);
				}
				$deleteId = $documentTable->deleteRow($deleteDocumentId,$instanceId);
				if (isset($deleteId)) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_DOCUMENT_SUCCESSFULL'));
				} else {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_DOCUMENT_FAIL'));
				}
			}
		}
		//get document list for shipment
		$listDocumentShipment = $documentTable->getList($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_DOCUMENT_ITEM, $listDocumentShipment);
		$this->view->assign(Sss_Ultility_CommonViewParamater::OTHER_QUERY_STRING, '?' . 'shipmentid' . "=" . $shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::OTHER_CONTROLLER_NAME, Sss_Ultility_CommonViewParamater::SHIPMENTCONTROLLER);
		if($status != -1) {
			$this->view->assign(Sss_Ultility_CommonViewParamater::OTHER_QUERY_STRING, '?' . 'shipmentid' . "=" . $shipmentId. '&' . 'status' . "=" . Sss_Ultility_Setting::RMA);
		}
		if($status != -1) {
			//action button
			$actionbuttons = array(
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::ADDOTHER, "add-document", Resources::_('ADD_DOCUMENT')),
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::BACK, 'rma', Resources::_('BTN_BACK'))
			);
		} else {
			//action button
			$actionbuttons = array(
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::ADDOTHER, "add-document", Resources::_('ADD_DOCUMENT')),
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::BACK, 'shipments', Resources::_('BTN_BACK'))
			);
		}

		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::ADD, 'feature', 'action' ,$this->listRole(), true) != 1) {
			$actionbuttons = array(
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::BACK, 'shipments', Resources::_('BTN_BACK'))
			);
			if($status != -1) {
				$actionbuttons = array(
				new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::BACK, 'rma', Resources::_('BTN_BACK'))
				);
			}
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);



		//get breadscrumb
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb(Resources::_('DOCUMENT'), '#')
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}

	public function editShipmentAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;
		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::EDIT, 'feature', 'action' ,$this->listRole(), true) != 1 ){
			$this->_redirect("/$handle/user/login");
		}


		$instanceTable        = new Application_Model_Mapper_Instance();
		$systemTypeTable      = new Shepherd_Model_Mapper_SystemType();
		$shipmentTable        = new Shepherd_Model_Mapper_Shipment();
		$stockLocationTable   = new Application_Model_Mapper_StockLocation();

		//get shipment type
		$shipmenttypeId = $this->getRequest()->getParam('systemtypeid', -1);

		$this->view->assign('shipmenttype', $shipmenttypeId);

		$shipmentTypes = $systemTypeTable->getSystemBySystemName(Sss_Ultility_SystemName::SHIPMENT_TYPE);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_TYPES, $shipmentTypes);

		$params = $this->getRequest()->getPost();

		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
			$shipmentId = $params['shipmentid'];
		$this->view->assign('shipmentid', $shipmentId);

		//get shipment type
		$status = $this->getRequest()->getParam('status', -1);
		if (($status == -1 || (int) $status == 0) && isset($params['status']))
			$status = $params['status'];
		$this->view->assign('status', $status);
		//get shipment by id
		$shipment = $shipmentTable->getShipmentById($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT, $shipment);

		$stockLocationDestination = $stockLocationTable->getAllStockLocation();
		if(isset($params['shipmenttype'])){
			$this->view->assign('shipmenttype', $params['shipmenttype']);

			//get stock locations
			if($params['shipmenttype'] == Sss_Ultility_Setting::RMA_SHIPMENT){
				$stockLocationDestination = $stockLocationTable->getAllStockLocation(null,Sss_Ultility_Setting::INTERNAL_STOCK);
			}
		}
		if($shipment->shipmenttype == Sss_Ultility_Setting::RMA_SHIPMENT){
			$stockLocationDestination = $stockLocationTable->getAllStockLocation(null,Sss_Ultility_Setting::INTERNAL_STOCK);
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::STOCKDESTINATIONS, $stockLocationDestination);

		//get stock locations
		$stockLocations = $stockLocationTable->getAllStockLocation();
		$this->view->assign(Sss_Ultility_CommonViewParamater::STOCKLOCATIONS, $stockLocations);

		//get controller and action
		$strController  = Sss_Ultility_CommonViewParamater::SHIPMENTCONTROLLER;
		$strAction      = 'shipments';
		$inventoryParam = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::INVENTORY, -1);
		if (($inventoryParam == -1) && isset($params[Sss_Ultility_CommonViewParamater::INVENTORY]))
			$inventoryParam = $params[Sss_Ultility_CommonViewParamater::INVENTORY];
		$this->view->assign(Sss_Ultility_CommonViewParamater::INVENTORY, $inventoryParam);

		if ($inventoryParam != -1 && $inventoryParam == 0) {
			$strController = Sss_Ultility_CommonViewParamater::SHIPMENTCONTROLLER;
			$strAction     = 'shipments';
			$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
			//check permission
			if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_DYNAMIC_MONITORING, Sss_Ultility_CommonViewParamater::ALL, 'feature', 'action' ,$this->listRole(), true) != 1 ){
				$this->_redirect("/$handle/user/login");
			}
		}
		if($status != -1){
			$strAction = "rma";
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::CONTROLLER, $strController);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION, $strAction);
		//get instance for check when not found
		$instance = $instanceTable->getInstanceByHandle($handle);
		//get stock location 
		$stockLocation = $stockLocationTable->getAllStockLocationByShepherd(Sss_Ultility_Setting::INTERNAL_STOCK, $instance->instanceid);
		$ArrStockLocation = array();
		$dataAuto = '';
		foreach($stockLocation as $sl){
			$ArrStockLocation[$sl['stocklocationname']]= $sl['stocklocationid'];
		}
		foreach ($ArrStockLocation as $key=>$value) {
			$dataAuto = $dataAuto."$key|$value#";
		}
		$dataAuto = substr($dataAuto, 0 ,strlen($dataAuto) - 1);
		$this->view->assign(Sss_Ultility_CommonViewParamater::AUTOCOMPLETE, $dataAuto);

		//edit shipment
		if (isset($params[Sss_Ultility_CommonViewParamater::BTN_SAVE]) && $shipmentId != -1) {
			$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
			if ($userInformation->username) {
				if(isset($params['txtOrigin']) && !empty($params['txtOrigin'])){
					$orgin = $params['origin'];
					$stockLocationByOrigin =  $stockLocationTable->select($orgin);
					if(trim($stockLocationByOrigin->stocklocationname) != trim($params['txtOrigin'])){
						$orgin = $params['txtOrigin'] ;
					}
					if(empty($orgin)){
						$orgin = $params['txtOrigin'] ;
					}
					if(empty($orgin)){
						$orgin = 0; 
					}
				} else{
					$orgin = 0;
				}
				if(isset($params['txtDestination']) && !empty($params['txtDestination'])){
					$destination = $params['destination'];
					$stockLocationByDestination =  $stockLocationTable->select($destination);
					if(trim($stockLocationByDestination->stocklocationname) != trim($params['txtDestination'])){
						$destination = $params['txtDestination'] ;
					}
					if(empty($destination)){
						$destination = $params['txtDestination'] ;
					}
					if(empty($destination)){
						$destination = 0; 
					}
				} else{
					$destination = 0;
				}
				$createdBy     = $userInformation->accountid;
				// Process update shipment
				$shipmentUpdate = $shipmentTable->findById($shipmentId,$instanceId);
				$referenceNumber = empty($params['reference_number']) ? null : addslashes($params['reference_number']);
				$supplierRef = empty($params['supplier_ref']) ? null : addslashes($params['supplier_ref']);
				$dateOfEntry = empty($params['date_of_entry']) ? null : Sss_Ultility_Common::getDateTimeNumber($params['date_of_entry']);
				$shipmentUpdate->setShipmentName(addslashes($params['shipmentname']));
				$shipmentUpdate->setShipDate(Sss_Ultility_Common::getDateTimeNumber($params['ship_date']));
				$shipmentUpdate->setOrigin(is_numeric($orgin) ? $orgin : 0);
				$shipmentUpdate->setDestination(is_numeric($destination) ? $destination : 0);
				$shipmentUpdate->setOriginText(is_numeric($orgin) ? null : $orgin);
				$shipmentUpdate->setDestinationText(is_numeric($destination) ? null : $destination);
				$shipmentUpdate->setInternalRef(addslashes($params['internal_ref']));
				$shipmentUpdate->setReferenceNumber($referenceNumber);
				$shipmentUpdate->setSupplierRef($supplierRef);
				$shipmentUpdate->setStatus(1);
				$shipmentUpdate->setShipmentType($params['shipmenttype']);
				$shipmentUpdate->setDescription(trim(addslashes($params['description'])));
				$shipmentUpdate->setDateOfEntry($dateOfEntry);
				$shipmentUpdate->setRevenue(trim(addslashes($params['revenue'])));
				$shipmentUpdate->setVolume(is_numeric($params['volume']) ? $params['volume'] : 0);
				$shipmentUpdate->setCreatedBy($createdBy);
				$shipmentUpdate->setUpdatedBy($createdBy);
				$shipmentUpdate->setCreatedDate(Sss_Ultility_Common::getDateTimeNumber());
				$shipmentUpdate->setUpdatedDate(Sss_Ultility_Common::getDateTimeNumber());
				if($ogrin == $destination && $ogrin != 0 && $destination != 0) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('SHIPMENT_NOT_HAVE_TWO_ADDRESS_SAME'));
				} else {
					$shipmentId    = $shipmentTable->save($shipmentUpdate,$instanceId);
					if (isset($shipmentId)) {
						$this->redirect(Resources::_('EDIT_SHIPMENT_SUCCESSFULL'), $this->view->url(array(
						Sss_Ultility_CommonViewParamater::CONTROLLER => $strController,
						Sss_Ultility_CommonViewParamater::ACTION => $strAction
						)) . $queryString);
					} else {
						$this->redirect(Resources::_('EDIT_SHIPMENT_FAIL'), $this->view->url(array(
						Sss_Ultility_CommonViewParamater::CONTROLLER => $strController,
						Sss_Ultility_CommonViewParamater::ACTION => $strAction
						)) . $queryString);
					}
				}
			}
		}

		//get breadscrumb
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb(Resources::_('EDIT_SHIPMENT'), '#')
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}
		
	public function addShipmentAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);

		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::ADD, 'feature', 'action' ,$this->listRole(), true) != 1 ){
			$this->_redirect("/$handle/user/login");
		}



		$systemTypeTable = new Shepherd_Model_Mapper_SystemType();
		$shipmentTable   = new Shepherd_Model_Mapper_Shipment();
		$shipmentContructor = new Shepherd_Model_Shipment();

		$instanceTable  = new Application_Model_Mapper_Instance();
		$stockLocationTable   = new Application_Model_Mapper_StockLocation();

		//get shipment type
		$shipmenttypeId = $this->getRequest()->getParam('systemtypeid', -1);

		$this->view->assign('shipmenttype', $shipmenttypeId);

		$shipmentTypes = $systemTypeTable->getSystemBySystemName(Sss_Ultility_SystemName::SHIPMENT_TYPE);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_TYPES, $shipmentTypes);

		//get monitor id
		$monitorId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::MONITORING_LOCALIZATION, -1);
		$this->view->assign(Sss_Ultility_CommonViewParamater::MONITOR, $monitorId);

		//insert shipment
		$params = $this->_request->getParams();

		//get shipment type
		$status = $this->getRequest()->getParam('status', -1);
		if (($status == -1 || (int) $status == 0) && isset($params['status']))
			$status = $params['status'];
		$this->view->assign('status', $status);

		$stockLocationDestination = $stockLocationTable->getAllStockLocation();
		if(isset($params['shipmenttype'])){
			$this->view->assign('shipmenttype', $params['shipmenttype']);
			//get stock locations
			if($params['shipmenttype'] == Sss_Ultility_Setting::RMA_SHIPMENT){
				$stockLocationDestination = $stockLocationTable->getAllStockLocation(null,Sss_Ultility_Setting::INTERNAL_STOCK);
			}
		}
		if($status == Sss_Ultility_Setting::RMA_SHIPMENT){
			$stockLocationDestination = $stockLocationTable->getAllStockLocation(null,Sss_Ultility_Setting::INTERNAL_STOCK);
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::STOCKDESTINATIONS, $stockLocationDestination);

		//get instance for check when not found
		$instance = $instanceTable->getInstanceByHandle($handle);

		//get stock locations
		$stockLocations = $stockLocationTable->getAllStockLocation();
		$this->view->assign(Sss_Ultility_CommonViewParamater::STOCKLOCATIONS, $stockLocations);

		//get stock location
		$stockLocation  = $stockLocationTable->select($instance->stocklocation);
		$this->view->assign('origin', $stockLocation->stocklocationid);
		$this->view->assign('originname', $stockLocation->stocklocationname);
		$rmaNumber = $shipmentTable->getCountShipmentByType(Sss_Ultility_Setting::RMA_SHIPMENT, Sss_Ultility_Common::getDateTimeNumber(date(DATEFORMAT)));
		$rmaNumber = $rmaNumber + 1;
		if($rmaNumber < 10){
			$rmaNumber = '00'.$rmaNumber;
		} else if($rmaNumber < 100) {
			$rmaNumber = '0'.$rmaNumber;
		}
			$rmaNumber = 'OVSRMA-'.date('ymd').'-'.$rmaNumber;
		$this->view->assign(Sss_Ultility_CommonViewParamater::RMA_SHIPMENT_COUNT, $rmaNumber);

		//get stock location 
		$stockLocation = $stockLocationTable->getAllStockLocationByShepherd(Sss_Ultility_Setting::INTERNAL_STOCK, $instance->instanceid);
		$ArrStockLocation = array();
		$dataAuto = '';
		foreach($stockLocation as $sl)
		{
			$ArrStockLocation[$sl['stocklocationname']]= $sl['stocklocationid'];
		}
		foreach ($ArrStockLocation as $key=>$value) {
			$dataAuto = $dataAuto."$key|$value#";
		}
		$dataAuto = substr($dataAuto, 0 ,strlen($dataAuto) - 1);
		$this->view->assign(Sss_Ultility_CommonViewParamater::AUTOCOMPLETE, $dataAuto);

		//get name when page load
		$shipmentName = $this->getValueRequest('shipmentname', $this->getRequest()->getParam('shipmentname', Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::SHIPMENT_SESSION);
		$this->view->assign('shipmentname', $shipmentName);
		if (isset($params[Sss_Ultility_CommonViewParamater::BTN_SAVE])) {
			$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
			if ($userInformation->username) {
				$createdBy  = $userInformation->accountid;
				// Process adding new shipment
				if(isset($params['txtOrigin']) && !empty($params['txtOrigin'])){
					$orgin = $params['origin'];
					if(empty($orgin)){
						$orgin = $params['txtOrigin'] ;
					}
					if(empty($orgin)){
						$orgin = 0; 
					}
				} else{
					$orgin = 0;
				}

				if(isset($params['txtDestination']) && !empty($params['txtDestination'])){
					$destination = $params['destination'];
					if(empty($destination)){
						$destination = $params['txtDestination'] ;
					}
					if(empty($destination)){
						$destination = 0; 
					}
				} else{
					$destination = 0;
				}
				$referenceNumber = empty($params['reference_number']) ? null : addslashes($params['reference_number']);
				$supplierRef = empty($params['supplier_ref']) ? null : addslashes($params['supplier_ref']);
				$dateOfEntry = empty($params['date_of_entry']) ? null : Sss_Ultility_Common::getDateTimeNumber($params['date_of_entry']);
				$shipmentContructor->setShipmentName(addslashes($params['shipmentname']));
				$shipmentContructor->setShipDate(Sss_Ultility_Common::getDateTimeNumber($params['ship_date']));
				$shipmentContructor->setOrigin(is_numeric($orgin) ? $orgin : 0);
				$shipmentContructor->setDestination(is_numeric($destination) ? $destination : 0);
				$shipmentContructor->setOriginText(is_numeric($orgin) ? null : $orgin);
				$shipmentContructor->setDestinationText(is_numeric($destination) ? null : $destination);
				$shipmentContructor->setInternalRef(addslashes($params['internal_ref']));
				$shipmentContructor->setReferenceNumber($referenceNumber);
				$shipmentContructor->setSupplierRef($supplierRef);
				$shipmentContructor->setStatus(1);
				$shipmentContructor->setShipmentType($params['shipmenttype']);
				$shipmentContructor->setDescription(trim(addslashes($params['description'])));
				$shipmentContructor->setDateOfEntry($dateOfEntry);
				$shipmentContructor->setRevenue(trim(addslashes($params['revenue'])));
				$shipmentContructor->setVolume(is_numeric($params['volume']) ? $params['volume'] : 0);
				$shipmentContructor->setCreatedBy($createdBy);
				$shipmentContructor->setUpdatedBy($createdBy);
				$shipmentContructor->setCreatedDate(Sss_Ultility_Common::getDateTimeNumber());
				$shipmentContructor->setUpdatedDate(Sss_Ultility_Common::getDateTimeNumber());

				$this->getValueRequest('shipmentname', '', Sss_Ultility_SessionName::SHIPMENT_SESSION);
				if(isset($params['origin']) && isset($params['destination']) && $params['origin'] == $params['destination'] && $params['origin'] != '' && $params['destination'] != ''){
					$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('SHIPMENT_NOT_HAVE_TWO_ADDRESS_SAME'));
				} else { 
					$shipmentId = $shipmentTable->save($shipmentContructor,$instance->instanceid);
					if (isset($shipmentId)) {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('ADD_SHIPMENT_SUCCESSFULL'));
					} else {
						$this->view->assign(Sss_Ultility_CommonViewParamater::ERRORMSG, Resources::_('ADD_SHIPMENT_FAIL'));
					}
					$queryString = '?shipmentid='.$shipmentId.'&'.Sss_Ultility_CommonViewParamater::MONITORING_LOCALIZATION . '=1';
					if($monitorId == 1){
						$this->redirect('', $this->view->url(array(
						Sss_Ultility_CommonViewParamater::CONTROLLER => $this->view->controllerName,
						Sss_Ultility_CommonViewParamater::ACTION => 'shipment-hardware'
						)) . $queryString);
					} elseif ($status != -1 || $params['shipmenttype'] == Sss_Ultility_Setting::RMA){
						$this->_forward('rma','inventory');
					} else {
						$this->_forward('shipments');
					}
					return;
				}
			}
		}

		//get breadscrumb
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb(Resources::_('ADD_SHIPMENT'), '#')
		);
		if($status != -1)
		{
			$breadscrumbs = array(
			new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
			new Sss_CommonEntity_BreadScrumb(Resources::_('RMA'), '#'),
			new Sss_CommonEntity_BreadScrumb(Resources::_('ADD_NEW_RMA_SHIPMENT'), '#')
			); 
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}

	public function geomappingAction()
	{

		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		
		// get session instance Id
		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;

		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_GEOMAPPING, Sss_Ultility_CommonViewParamater::ALL, 'feature', 'action' ,$this->listRole(), true) != 1 ) {
			$this->_redirect("/$handle/monitoring-localization/configuration");
		}
		//get breadscrumb
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb('Geomapping', '#')
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);

		$paramItemId     = $this->_request->getParam('itemid', 'underfine');
		$paramShipmentId = $this->_request->getParam('shipmentid', 'underfine');

		if ($paramItemId > 0) {
			//get address from item table to show on map
			$itemTable    = Sss_Service_Locator::getInstance()->getService($this->_getShepherdTable('Item'));
			$dataItemById = $itemTable->getItemById($paramItemId);

			$s = "var markers =[";
			$s .= "{";
			$s .= "'name':'" . $dataItemById->itemname . "',";
			$s .= "'address':'" . $dataItemById->address . "',";
			$s .= "'post':'" . $dataItemById->postcode . "',";
			$s .= "'city':'" . $dataItemById->city . "',";
			$s .= "'country':'" . $dataItemById->country . "',";
			$s .= "'created_date':'" . date(DATETIMEFORMAT, $dataItemById->created_date) . "',";
			$s .= "'updated_date':'" . ($dataItemById->updated_date > 0 ? date(DATETIMEFORMAT, $dataItemById->updated_date) : '0') . "'";
			$s .= "},";
			$s .= "];";
		} elseif ($paramShipmentId > 0) { 
			//get address from shipment table to show on map
			$stocklocationTable = new Application_Model_Mapper_StockLocation();
			$dataStocklocationById = $stocklocationTable->getListFromShipment($paramShipmentId,$instanceId);
			$s = "var markers =[";
			foreach ($dataStocklocationById as $dataStock) {
				$s .= "{";
				$s .= "'locationid':'" . $dataStock['stocklocationid'] . "',";
				$s .= "'name':'" . $dataStock['shipmentname'] . "',";
				$s .= "'address':'" . $dataStock['address'] . "',";
				$s .= "'post':'" . $dataStock['postcode'] . "',";
				$s .= "'city':'" . $dataStock['city'] . "',";
				$s .= "'country':'" . $dataStock['country'] . "',";
				$s .= "'ship_date':'" . date($dataStock['ship_date']) . "',";
				$s .= "'internal_ref':'" . $dataStock['internal_ref'] . "',";
				$s .= "'reference_number':'" . $dataStock['reference_number'] . "',";
				$s .= "'supplier_ref':'" . $dataStock['supplier_ref'] . "'";
				$s .= "},";
			}
			$s .= "];";
		} else {
			$s = "var markers ='';";
		}
		$this->view->assign("s", $s);
		$this->view->assign("itemid", $paramItemId);
		$this->view->assign("shipmentid", $paramShipmentId);
	}
		
	public function addProductAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$productTable = new Shepherd_Model_Mapper_Product();
		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;
		$params     = $this->getRequest()->getPost();
		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);//get shipmentid
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid'])){
			$shipmentId = $params['shipmentid'];
		}
		$this->view->assign('shipmentid', $shipmentId);
		$productId = $this->getRequest()->getParam('productid', -1);
		if (($productId == -1 || (int) $productId == 0) && isset($params['productid'])){
			$productId = $params['productid'];
		}
		$this->view->assign('productid', $productId);
		$shipmentdetail = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL, -1);
		if (($shipmentdetail == -1 || (int) $shipmentdetail == 0) && isset($params[Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL])){
			$shipmentdetail = $params[Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL];
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL, $shipmentdetail);
		$strAction   = 'product';
		$queryString = "?shipmentid=" . $shipmentId;
		if ($shipmentdetail != -1) {
			$strAction   = Sss_Ultility_CommonViewParamater::ACTION_SHIPMENT_DETAIL;
			$queryString = "?shipmentid=" . $shipmentId . '&' . Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL . '=1';
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::QUERY_STRING, $queryString);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION, $strAction);
		//get product edit
		$product = $productTable->getProductById($productId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $product);
		$actionName = '';
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::EDIT, 'feature', 'action' ,$this->listRole(), true) != 1 ){
			$this->_redirect("/$handle/user/login");
		}
		$actionName = ($productId != -1) ? Resources::_('EDIT_PRODUCT') : Resources::_('ADD_PRODUCT');
		if ($userInformation->username && isset($params[Sss_Ultility_CommonViewParamater::BTN_SAVE])) {
			// Process add/edit new product
			if ($productId != -1) {
				$producUpdate = $productTable->findById($productId,$instanceId);
				$producUpdate->setProductName($params['productname']);
				$producUpdate->setAmount($params['amount']);
				$producUpdate->setLotNumber($params['lot_number']);
				$producUpdate->setValuetype($params['valuetype']);
				$producUpdate->setComment($params['comment']);
				$producUpdate->setShipment($shipmentId);
				$id           = $productTable->save($producUpdate,$instanceId);
				$message = isset($id) ? Resources::_('EDIT_PRODUCT_SUCCESSFULL') : Resources::_('EDIT_PRODUCT_FAIL');
				$this->redirect($message, $this->view->url(array(
				Sss_Ultility_CommonViewParamater::CONTROLLER => $this->view->controllerName,
				Sss_Ultility_CommonViewParamater::ACTION => $strAction
				)) . $queryString);
			} else {
				$producUpdate = new Shepherd_Model_Product();
				$producUpdate->setProductName($params['productname']);
				$producUpdate->setAmount($params['amount']);
				$producUpdate->setLotNumber($params['lot_number']);
				$producUpdate->setValuetype($params['valuetype']);
				$producUpdate->setComment($params['comment']);
				$producUpdate->setShipment($shipmentId);
				$id           = $productTable->save($producUpdate,$instanceId);
				$message = isset($id) ? Resources::_('ADD_PRODUCT_SUCCESSFULL') : Resources::_('ADD_PRODUCT_FAIL');
				$this->redirect($message, $this->view->url(array(
				Sss_Ultility_CommonViewParamater::CONTROLLER => $this->view->controllerName,
				Sss_Ultility_CommonViewParamater::ACTION => $strAction
				)) . $queryString);
			}
		}
		//get breadscrumb
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb(Resources::_('PRODUCT'), '#'),
		new Sss_CommonEntity_BreadScrumb($actionName, '#')
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}

	public function addDocumentAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$documentTable = new Shepherd_Model_Mapper_Document();
		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;

		//get shipmentid
		$params     = $this->getRequest()->getPost();
		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
			$shipmentId = $params['shipmentid'];
		$this->view->assign('shipmentid', $shipmentId);

		//get shipment type
		$status = $this->getRequest()->getParam('status', -1);
		if (($status == -1 || (int) $status == 0) && isset($params['status']))
			$status = $params['status'];
		$this->view->assign('status', $status);

		$documentId = $this->getRequest()->getParam('documentid', -1);
		if (($documentId == -1 || (int) $documentId == 0) && isset($params['documentid']))
			$documentId = $params['documentid'];
		$this->view->assign('documentid', $documentId);

		$shipmentdetail = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL, -1);
		if (($shipmentdetail == -1 || (int) $shipmentdetail == 0) && isset($params[Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL]))
			$shipmentdetail = $params[Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL];
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL, $shipmentdetail);

		$strAction   = 'document';
		$queryString = "?shipmentid=" . $shipmentId;
		if($status != -1){
			$queryString = "?shipmentid=" . $shipmentId . '&status=' . Sss_Ultility_Setting::RMA; 
		}
		if ($shipmentdetail != -1) {
			$strAction   = Sss_Ultility_CommonViewParamater::ACTION_SHIPMENT_DETAIL;
			$queryString = "?shipmentid=" . $shipmentId . '&' . Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL . '=1';
			if($status != -1){
				$strAction   = 'rma-detail';
				$queryString = "?shipmentid=" . $shipmentId . '&' . Sss_Ultility_CommonViewParamater::SHIPMENT_DETAIL . "=1&status=" . Sss_Ultility_Setting::RMA;  
			}
		}
		$this->view->assign(Sss_Ultility_CommonViewParamater::QUERY_STRING, $queryString);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION, $strAction);
		//get document edit
		$document = $documentTable->getDocumentById($documentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $document);
		$actionName = '';
		if ($documentId != -1) {
			//check permission
			if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::EDIT, 'feature', 'action' ,$this->listRole(), true) != 1 ){
				$this->_redirect("/$handle/user/login");
			}
			$actionName = Resources::_('EDIT_DOCUMENT');
		} else {
			//check permission
			if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_SHIPMENT, Sss_Ultility_CommonViewParamater::ADD, 'feature', 'action' ,$this->listRole(), true) != 1 ) {
				$this->_redirect("/$handle/user/login");
			}
			$actionName = Resources::_('ADD_DOCUMENT');
		}
		if (isset($params[Sss_Ultility_CommonViewParamater::BTN_SAVE])) {
			if ($userInformation->username) {
				//upload document
				$fullPath = null;
				$fileName = $_FILES[Sss_Ultility_CommonViewParamater::FILE_UPLOAD][Sss_Ultility_CommonViewParamater::NAME];
				if ($fileName) {
					$filePath     = $_FILES[Sss_Ultility_CommonViewParamater::FILE_UPLOAD][Sss_Ultility_CommonViewParamater::TMP_NAME];
					$newPath      = Sss_Ultility_CommonViewParamater::FILE_PATH_DOCUMENT . $fileName;
					move_uploaded_file($filePath, $newPath);
					$fullPath     = '/' . Sss_Ultility_CommonViewParamater::FILE_PATH_DOCUMENT . $fileName;
				}
				$createdBy = $userInformation->accountid;
				// Process add/edit new document
				if ($documentId != -1) {
					$documentUpdate = $documentTable->findById($documentId,$instanceId);
					$documentUpdate->setDocumentName($params['documentname']);
					$documentUpdate->setUpdatedDate(Sss_Ultility_Common::getDateTimeNumber());
					$documentUpdate->setUpdatedBy($createdBy);
					$documentUpdate->setShipment($shipmentId);
					if($fileName) {
						if ($document->path) {
							if (file_exists(getcwd() . $document->path))
								unlink(getcwd() . $document->path);
						}
						$documentUpdate->setPath($fullPath);
					}
					$id = $documentTable->save($documentUpdate,$instanceId);
					$message = isset($id) ? Resources::_('EDIT_DOCUMENT_SUCCESSFULL') : Resources::_('EDIT_DOCUMENT_FAIL');
					$this->redirect($message, $this->view->url(array(
					Sss_Ultility_CommonViewParamater::CONTROLLER => $this->view->controllerName,
					Sss_Ultility_CommonViewParamater::ACTION => $strAction
					)) . $queryString);
				} else {
					$documentData = new Shepherd_Model_Document();
					$documentData->setDocumentName($params['documentname']);
					$documentData->setCreatedDate(Sss_Ultility_Common::getDateTimeNumber());
					$documentData->setUpdatedDate(Sss_Ultility_Common::getDateTimeNumber());
					$documentData->setCreatedBy($createdBy);
					$documentData->setUpdatedBy($createdBy);
					$documentData->setShipment($shipmentId);
					if($fileName) {
						if ($document->path) {
							if (file_exists(getcwd() . $document->path))
								unlink(getcwd() . $document->path);
						}
						$documentData->setPath($fullPath);
					}
					$id = $documentTable->save($documentData,$instanceId);
					$message = isset($id) ? Resources::_('ADD_DOCUMENT_SUCCESSFULL') : Resources::_('ADD_DOCUMENT_FAIL');
					$this->redirect($message, $this->view->url(array(
					Sss_Ultility_CommonViewParamater::CONTROLLER => $this->view->controllerName,
					Sss_Ultility_CommonViewParamater::ACTION => $strAction
					)) . $queryString);
				}
			}
		}
		$title = Resources::_('SHIPMENT');
		if($status != -1){
			$title = Resources::_('RMA');
		}
		//get breadscrumb
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb(Resources::_('DOCUMENT'), '#'),
		new Sss_CommonEntity_BreadScrumb($actionName, '#')
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}
	
	public function shipmentExportAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_DYNAMIC_MONITORING, Sss_Ultility_CommonViewParamater::ALL, 'feature', 'action' ,$this->listRole(), true) != 1 ){
			$this->_redirect("/$handle/user/login");
		}
		if ($userInformation) {

			$shipmentTable   = new Shepherd_Model_Mapper_Shipment();
			$hardwareTable   = new Application_Model_Mapper_Hardware();
			$alertTable      = new Shepherd_Model_Mapper_Alert();




			//get order, page, limit param
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD] = isset($get[Sss_Ultility_CommonViewParamater::ORDERFIELD]) ? $get[Sss_Ultility_CommonViewParamater::ORDERFIELD] : null;
			$filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE]  = isset($get[Sss_Ultility_CommonViewParamater::ORDERTYPE]) ? $get[Sss_Ultility_CommonViewParamater::ORDERTYPE] : null;
			$filterParam[Sss_Ultility_CommonViewParamater::PAGE] = isset($get[Sss_Ultility_CommonViewParamater::PAGE]) ? $get[Sss_Ultility_CommonViewParamater::PAGE] : 1;
			//test param filter: omit title text in field
			$paramShipmentName = strcmp($this->getRequest()->getParam('shipmentname', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('SHIPMENT_NAME_FILTER')) == 0 ? '' : $this->getRequest()->getParam('shipmentname', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramOriginal     = strcmp($this->getRequest()->getParam('origin', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('SHIPMENT_ORGIN_FILTER')) == 0 ? '' : $this->getRequest()->getParam('origin', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramDestination  = strcmp($this->getRequest()->getParam('destination', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('SHIPMENT_DESTINATION')) == 0 ? '' : $this->getRequest()->getParam('destination', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramFromDate     = strcmp($this->getRequest()->getParam('date_from', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('FROM_DATE')) == 0 ? '' : $this->getRequest()->getParam('date_from', Sss_Ultility_CommonViewParamater::UNDERFINE);
			$paramToDate       = strcmp($this->getRequest()->getParam('date_to', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('TO_DATE')) == 0 ? '' : $this->getRequest()->getParam('date_to', Sss_Ultility_CommonViewParamater::UNDERFINE);



			//get harware filter
			$filterParam['shipmentname'] = $this->getValueRequest('shipmentname', trim($paramShipmentName), Sss_Ultility_SessionName::DYNAMICMONITORING_SESSION);
			$filterParam['origin']         = $this->getValueRequest('origin', trim($paramOriginal), Sss_Ultility_SessionName::DYNAMICMONITORING_SESSION);
			$filterParam['destination']   = $this->getValueRequest('destination', trim($paramDestination), Sss_Ultility_SessionName::DYNAMICMONITORING_SESSION);
			$filterParam['date_from']     = $this->getValueRequest('date_from', $paramFromDate, Sss_Ultility_SessionName::DYNAMICMONITORING_SESSION);
			$filterParam['date_to']       = $this->getValueRequest('date_to', $paramToDate, Sss_Ultility_SessionName::DYNAMICMONITORING_SESSION);
			
			$filterParam['shipment_history'] = 0;
			//get sensor list by shipment
			$hardwareByShipments = $hardwareTable->getHardwareByDestinationGraph('', Sss_Ultility_SystemName::SHIPMENTS);
			$this->view->assign(Sss_Ultility_CommonViewParamater::HARDWARE_BY_SHIPMENT, $hardwareByShipments);

			//get sensor list by item
			$hardwareByItems = $hardwareTable->getHardwareByDestinationGraph('', Sss_Ultility_SystemName::ITEMS);
			$this->view->assign(Sss_Ultility_CommonViewParamater::HARDWARE_BY_ITEM, $hardwareByItems);
			//get hardware list
			$checkShipmentSession = new Zend_Session_Namespace('checkShipment');
			$checkDynamicSession = new Zend_Session_Namespace('checkDynamic');
			$checkShipment = $checkShipmentSession->checkShipment;
			$checkDynamic = $checkDynamicSession->checkDynamic;
			if(!$checkShipment && $checkDynamic) {
				$list = $shipmentTable->getListItemShipmentDynamicMonitoring(-1, 0, $filterParam,0);
				$rowAll  = $list->list;
				
			}elseif($checkShipment && !$checkDynamic) {
				$list       = $shipmentTable->getList(-1, 0, $filterParam);
				$rowAll        = $list->list;
				
			}else {
				$list = $shipmentTable->getListItemShipmentDynamicMonitoring(-1, 0, $filterParam,1);
				$rowAll  = $list->list;
				
			}
			$pagination = $list->pagination;
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $rowAll);
			$this->view->assign(Sss_Ultility_CommonViewParamater::PAGINATION, $pagination);

			//get alert list for shipment
			$listAlertShipment = $alertTable->getList(Sss_Ultility_SystemName::SHIPMENTS);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_ALERT_SHIPMENT, $listAlertShipment);

			//get alert list for item
			$listAlertItem = $alertTable->getList(Sss_Ultility_SystemName::ITEMS);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_ALERT_ITEM, $listAlertItem);

			$count = ((($pagination->pagecurrent * Sss_Ultility_Setting::PAGE_SIZE) - Sss_Ultility_Setting::PAGE_SIZE));                      $columnNames = array(
			Resources::_('ID'),
			Resources::_('SHIPMENT_NAME_FILTER'),
			Resources::_('SHIPMENT_ORGIN_LIST'),
			Resources::_('SHIPMENT_DESTINATION_LIST'),
			Resources::_('DATE'),
			Resources::_('GRAPHS'),
			Resources::_('ALERTS')
			);

			$arrData = array();
			$index = 0;
			foreach((array)$rowAll as $row) {
				$listGraph = '';
				$listAlert = '';
				if($row['isitem'] == 1){
					foreach($hardwareByItems as $typeName=>$hardwareByItem){
						if($hardwareByItem['destination'] == $row['shipmentid']){
							$listGraph .= $hardwareByItem['typename'].", ";
						}
					}
					foreach ($listAlertItem as $rowalert) {
						if($row['shipmentid'] == $rowalert['destination']){
							$listAlert .= $rowalert['alertname'].", ";
						}
					}
				} else {
					foreach($hardwareByShipments as $typeName=>$hardwareByShipment){
						if($hardwareByShipment['destination'] == $row['shipmentid']){
							$listGraph .= $hardwareByShipment['typename'].", ";
						}
					}
					foreach ($listAlertShipment as $rowalert) {
						if($row['shipmentid'] == $rowalert['destination']){
							$listAlert .= $rowalert['alertname'].", ";
						}
					}
				}
				if($listGraph){
					$listGraph = substr($listGraph,0,strlen($listGraph)-2);
				}
				if($listAlert){
					$listAlert = substr($listAlert,0,strlen($listAlert)-2);
				}
				$count++;
				$arrData[$index][] = $count;
				$arrData[$index][] = $row['shipmentname'];
				$arrData[$index][] = $row['origin'];
				$arrData[$index][] = $row['destination'];
				$arrData[$index][] = Sss_Ultility_Common::getDateTime(DATETIMEFORMAT, $row['ship_date']);
				$arrData[$index][] = $listGraph;
				$arrData[$index][] = $listAlert;
				$index++;
			}
			$isPrint = isset($_GET['print']) && $_GET['print'] !=''? true:false;
			if($isPrint) return;
			if($checkShipment && $checkDynamic) {
				$this->exportDataToExcel($columnNames, $arrData, Resources::_('SHIPMENT_DYNAMICMONITORING'));
			}else if(!$checkShipment && $checkDynamic) {
				$this->exportDataToExcel($columnNames, $arrData, Resources::_('DYNAMIC_MONITORING'));
			}else {
				$this->exportDataToExcel($columnNames, $arrData, Resources::_('SHIPMENT'));
			}
		}
	}

	public function addItemAction()
	{
		$userInformation = new Zend_Session_Namespace(Sss_Ultility_SessionName::ACCOUNT_SESSION_SHEPHERD . $this->getCurrentHandle());
		$handle = $this->getRequest()->getParam('handle', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$defaultOvisNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::OVIS_SHEPHERD_SYSTEM . $handle);
		$instanceId = $defaultOvisNamespace->dbShepherd;
		//check permission
		if(Sss_Ultility_Common::in_array_field(Sss_Ultility_CommonViewParamater::PER_DYNAMIC_MONITORING, Sss_Ultility_CommonViewParamater::ALL, 'feature', 'action' ,$this->listRole(), true) != 1 ) {
			$this->_redirect("/$handle/user/login");
		}
		$hardwareTable   = new Application_Model_Mapper_Hardware();
		$itemTable       = new Shepherd_Model_Mapper_Item();
		$allocationTable = new Shepherd_Model_Mapper_Allocation();
		$stockLocationInstanceTable = new Application_Model_Mapper_StockLocationInstance();
		$instanceTable  = new Application_Model_Mapper_Instance();

		//get params
		$params = $this->getRequest()->getParams();

		$itemid = $this->getValueRequest('itemid', $this->getRequest()->getParam('itemid', Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ITEM_SESSION);
		$getDataItem = $itemTable->getItemById($itemid);
		$createdBy        = '';
		if ($userInformation->username) {
			$createdBy = $userInformation->accountid;
		}
		$arrParamAssignHardwareId = $this->getRequest()->getParam('hardwareid', Sss_Ultility_CommonViewParamater::UNDERFINE);
		if ($arrParamAssignHardwareId != Sss_Ultility_CommonViewParamater::UNDERFINE){
			try{
				//assign hardware to item
				if ($arrParamAssignHardwareId && $userInformation->username) {
					$arrAssignHardwareId = explode('_', $arrParamAssignHardwareId);
					if (false != $arrAssignHardwareId) {
						for ($i = 1; $i < count($arrAssignHardwareId); $i++) {
							$valueHardware = $arrAssignHardwareId[$i];
							if ($valueHardware > 0 && $valueHardware != '') {
								$startdt = date(DATETIME_MYSQL_FORMAT,$getDataItem->created_date);
								$allocationContructor = new Shepherd_Model_Allocation();
								$allocationContructor->setHardware($valueHardware);
								$allocationContructor->setDestination($itemid);
								$allocationContructor->setStatus(1);
								$allocationContructor->setSystemName(Sss_Ultility_SystemName::ITEMS);
								$allocationContructor->setStartdt($startdt);
								$checkInsert   = $allocationTable->checkAssignTo($valueHardware, $itemid, Sss_Ultility_SystemName::ITEMS);
								if ($checkInsert == true) {
									$allocatinId = $allocationTable->save($allocationContructor,$instanceId);
									if($allocatinId){
										$item = $itemTable->getItemById($itemid);
										$hardware = $hardwareTable->getHardwareById($valueHardware);
										$hardwareUpdate = $hardwareTable->findById($valueHardware);
										$hardwareUpdate->setAllocation($hardware->stocklocationname.', '.$item->itemname);
										$hardwareTable->save($hardwareUpdate);
									}
								}
							}
						}
					}
				}
				$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('SENSOR_ATTACH_TO_ITEM_SUCCESSFULL'));
			} catch(Exception $e) {
				Sss_Ultility_Common::logger()->log($e, Zend_Log::ERR);
				$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('SENSOR_ATTACH_TO_ITEM_FAIL'));
			}
		}
		//get order, page, limit param
		$filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD] = $this->getValueRequest(Sss_Ultility_CommonViewParamater::ORDERFIELD, $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::ORDERFIELD, Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ITEM_SESSION);
		$filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE]  = $this->getValueRequest(Sss_Ultility_CommonViewParamater::ORDERTYPE, $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::ORDERTYPE, Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ITEM_SESSION);
		$filterParam[Sss_Ultility_CommonViewParamater::PAGE]       = $this->getValueRequest(Sss_Ultility_CommonViewParamater::PAGE, $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::PAGE, Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ITEM_SESSION);

		//test param filter: omit title text in field
		$paramHardwareId     = strcmp($this->getRequest()->getParam('hardwareid', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('ID')) == 0 ? '' : $this->getRequest()->getParam('hardwareid', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$paramHardwareName   = strcmp($this->getRequest()->getParam('hardwarename', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('HARDWARE_NAME')) == 0 ? '' : $this->getRequest()->getParam('hardwarename', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$paramHardwareSerial = strcmp($this->getRequest()->getParam('serial_number', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('SERIAL')) == 0 ? '' : $this->getRequest()->getParam('serial_number', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$paramHardwareVendor = strcmp($this->getRequest()->getParam('vendor', Sss_Ultility_CommonViewParamater::UNDERFINE), Resources::_('VENDOR')) == 0 ? '' : $this->getRequest()->getParam('vendor', Sss_Ultility_CommonViewParamater::UNDERFINE);

		//get harware filter
		$filterParam['hardwareid']   = $this->getValueRequest('hardwareid', $paramHardwareId, Sss_Ultility_SessionName::ITEM_SESSION);
		$filterParam['hardwarename'] = $this->getValueRequest('hardwarename', $paramHardwareName, Sss_Ultility_SessionName::ITEM_SESSION);
		$filterParam['serial_number'] = $this->getValueRequest('serial_number', $paramHardwareSerial, Sss_Ultility_SessionName::ITEM_SESSION);
		$filterParam['vendor']        = $this->getValueRequest('vendor', $paramHardwareVendor, Sss_Ultility_SessionName::ITEM_SESSION);

		$limit = $this->getValueRequest(Sss_Ultility_CommonViewParamater::LIMIT, $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::LIMIT, Sss_Ultility_CommonViewParamater::UNDERFINE), Sss_Ultility_SessionName::ITEM_SESSION);
		$defaultNamespace = new Zend_Session_Namespace(Sss_Ultility_SessionName::SHEPHERD_SYSTEM . $this->getCurrentHandle());
		$filterParam['stocklocation'] = $defaultNamespace->stocklocationId;

		$isAddNew    = false;
		$queryString = '';
		$paramRequestItemId = $this->getRequest()->getParam('itemid', Sss_Ultility_CommonViewParamater::UNDERFINE);
		if ($paramRequestItemId != Sss_Ultility_CommonViewParamater::UNDERFINE) {
			$queryString = '?itemid=' . $itemid;
			$isAddNew     = false;
			//get breadscrumb
			$breadscrumbs = array(
			new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
			new Sss_CommonEntity_BreadScrumb(Resources::_('EDIT_ITEM'), '#')
			);
			$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
		} else {
			$isAddNew = true;
			unset($_SESSION[Sss_Ultility_SessionName::ITEM_SESSION]);
			//get breadscrumb
			$breadscrumbs = array(
			new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
			new Sss_CommonEntity_BreadScrumb(Resources::_('ADD_NEW_ITEM'), '#')
			);
			$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
		}
		$instance = $instanceTable->getInstanceByHandle($handle);
		if($instance) {
			$stockLocations = $stockLocationInstanceTable->getStockLocationByInstance($instance->instanceid);
		}
		$stockLocationIds = '';
		if(count($stockLocations) > 0) {
			foreach($stockLocations as $s){
				if($stockLocationIds == '')  {
					$stockLocationIds =  $s['stocklocation'];
				} else {
					$stockLocationIds =  $stockLocationIds.','.$s['stocklocation'];
				}
			}
		}
		if($stockLocationIds != '') {
			$filterParam['stocklocation'] = $stockLocationIds;
		}
		//get hardware list
		$list       = $hardwareTable->getListForItem($limit, 0, $filterParam);
		$row        = $list->list;
		$pagination = $list->pagination;
		$this->view->assign(Sss_Ultility_CommonViewParamater::FILTER_PARAM, $filterParam);
		$this->view->assign(Sss_Ultility_CommonViewParamater::LIMIT, $limit);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $row);
		$this->view->assign(Sss_Ultility_CommonViewParamater::PAGINATION, $pagination);
		$this->view->assign(Sss_Ultility_CommonViewParamater::IS_ADD_NEW, $isAddNew);
		$this->view->assign(Sss_Ultility_CommonViewParamater::QUERY_STRING, $queryString);
		if ($itemid != Sss_Ultility_CommonViewParamater::UNDERFINE && $itemid != '' && $isAddNew == false) {
			if ($userInformation->username) {
				//update hardware when detach
				if(isset($params['hardwareid']) && isset($params['btnSaveHardware'])){
					$date = new DateTime();
					$enddt = $date->format(DATETIME_MYSQL_FORMAT);
					$hardwareId = $params['hardwareid'];
					$hardware = $hardwareTable->getHardwareById($hardwareId);
					$getAllocationId = $allocationTable->getById($itemid, Sss_Ultility_SystemName::ITEMS,null, $hardwareId);
					$allocationUpdate = $allocationTable->findById($getAllocationId->allocationid,$instanceId);
					$allocationUpdate->setStatus(0);
					$allocationUpdate->setNote($params['detach_location_name'.$hardwareId]);
					$allocationUpdate->setEnddt($enddt);
					$allocationTable->save($allocationUpdate,$instanceId);
					$hardwareUpdate = $hardwareTable->findById($hardwareId);
					$hardwareUpdate->setDetachLocationName($params['detach_location_name'.$hardwareId]);
					if((int)$params['stocklocationid']) {
						$hardwareUpdate->setDetachLocationId($params['detach_location_id']);
					}
					$id   = $hardwareTable->save($hardwareUpdate);
					if (isset($id)) {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DETACH_SENSOR_SUCCESSFULL'));
					} else {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DETACH_SENSOR_FAIL'));
					}
				}
				//remove sensor from shipment
				$deleteSensorId = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::DELETE_SENSOR_ID, -1);

				if (isset($deleteSensorId) && $deleteSensorId != -1) {
					$arrId    = array(
					'hardware = ?' => (int) $deleteSensorId,
					'destination = ?' => (int) $itemid,
					'systemname = ?' => Sss_Ultility_SystemName::ITEMS
					);
					$deleteId = $allocationTable->deleteRow(null, $arrId,$instanceId);
					if (isset($deleteId)) {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_HARDWARE_SUCCESSFULL'));
					} else {
						$this->view->assign(Sss_Ultility_CommonViewParamater::NOTICEMSG, Resources::_('DELETE_HARDWARE_FAIL'));
					}
				}
			}
			//get sensor list for item
			$listSensorItem = $hardwareTable->getListByDestination($itemid, Sss_Ultility_SystemName::ITEMS);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM, $listSensorItem);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM_COUNT, count($listSensorItem));
			$objItem = $itemTable->getItemById($itemid);
			$this->view->assign('itemid', $objItem->itemid);
			$this->view->assign('itemname', $objItem->itemname);
			$this->view->assign('address', $objItem->address);
			$this->view->assign('description', $objItem->description);
		}

		if($paramRequestItemId != Sss_Ultility_CommonViewParamater::UNDERFINE && count($row) > 0 && $row != false)
		{
			$actionbuttons = array(
			new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::SEARCH, 'javascript:hideShow(this);', Resources::_('SEARCH'))
			);
			$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);
		}
		//insert or update data
		if (false != $params) {
			if (isset($params[Sss_Ultility_CommonViewParamater::BTN_SAVE])) {
				if (empty($params['itemname'])) {
					$this->view->assign(Sss_Ultility_CommonViewParamater::ERRORMSG, Resources::_('REQUIRED_FIELD'));
					$this->_forward('shipments');
				} else {
					if ($params['itemid'] > 0 && $this->getRequest()->getParam('itemid', Sss_Ultility_CommonViewParamater::UNDERFINE) != Sss_Ultility_CommonViewParamater::UNDERFINE) {
						$itemUpdate = $itemTable->findById((int)$params['itemid'],$instanceId);
						$itemUpdate->setItemName(isset($params['itemname']) ? addslashes($params['itemname']) : '');
						$itemUpdate->setAddress(isset($params['address']) ? addslashes($params['address']) : '');
						$itemUpdate->setDescription(isset($params['address']) ? addslashes($params['description']) : '');
						$itemUpdate->setIsused(1);
						$itemUpdate->setUpdatedBy($createdBy);
						$itemUpdate->setUpdatedDate(time());
						$bResult = $itemTable->save($itemUpdate,$instanceId);
						if (!$bResult) {
							$this->getValueRequest(Sss_Ultility_CommonViewParamater::MSG, Resources::_('EDIT_ITEM_FAIL'), Sss_Ultility_SessionName::MSG_SESSION);
						} else {
							$this->redirect(Resources::_('EDIT_ITEM_SUCCESSFUL'), $this->view->url(array(
							Sss_Ultility_CommonViewParamater::CONTROLLER => $this->view->controllerName,
							Sss_Ultility_CommonViewParamater::ACTION => 'shipments'
							)));
						}
					} else {
						$itemContructor = new Shepherd_Model_Item();
						$itemContructor->setItemName(isset($params['itemname']) ? addslashes($params['itemname']) : '');
						$itemContructor->setAddress(isset($params['address']) ? addslashes($params['address']) : '');
						$itemContructor->setDescription(isset($params['address']) ? addslashes($params['description']) : '');
						$itemContructor->setIsused(1);
						$itemContructor->setUpdatedBy($createdBy);
						$itemContructor->setUpdatedDate(Sss_Ultility_Common::getDateTimeNumber());
						$itemContructor->setCreatedBy($createdBy);
						$itemContructor->setCreatedDate(Sss_Ultility_Common::getDateTimeNumber());
						//echo $arrItem; die;
						$insertItemId   = $itemTable->save($itemContructor,$instanceId);
						if ($insertItemId <= 0) {
							$this->getValueRequest(Sss_Ultility_CommonViewParamater::MSG, Resources::_('SAVE_ITEM_FAIL'), Sss_Ultility_SessionName::MSG_SESSION);
						} else {
							$this->redirect(Resources::_('SAVE_ITEM_SUCCESSFUL'), $this->view->url(array(
							Sss_Ultility_CommonViewParamater::CONTROLLER => $this->view->controllerName,
							Sss_Ultility_CommonViewParamater::ACTION => 'add-item'
							)).'?itemid='.$insertItemId);
						}
					}
				}
			}
		}
	}

	public function shipmentHistoryDetailAction()
	{
		$systemTypeTable = new Shepherd_Model_Mapper_SystemType();
		$shipmentTable   = new Shepherd_Model_Mapper_Shipment();
		$alertTable      = new Shepherd_Model_Mapper_Alert();
		$productTable    = new Shepherd_Model_Mapper_Product();
		$documentTable   = new Shepherd_Model_Mapper_Document();
		$hardwareTable   = new Application_Model_Mapper_Hardware();



		//get shipment type
		$shipmenttypeId = $this->getRequest()->getParam('systemtypeid', -1);
		$this->view->assign('shipmenttype', $shipmenttypeId);
		$shipmentTypes = $systemTypeTable->getSystemBySystemName(Sss_Ultility_SystemName::SHIPMENT_TYPE);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_TYPES, $shipmentTypes);

		//get shipmentid
		$params     = $this->getRequest()->getPost();
		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
			$shipmentId = $params['shipmentid'];
		$this->view->assign('shipmentid', $shipmentId);

		//get shipment by id
		$shipment = $shipmentTable->getShipmentById($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT, $shipment);

		//list alert for shipment detail


		//get alert list for shipment
		$listAlertShipment = $alertTable->getList(Sss_Ultility_SystemName::SHIPMENTS, $shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_ALERT_SHIPMENT, $listAlertShipment);

		//get product list for shipment
		$listProductShipment = $productTable->getList($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_PRODUCT_ITEM, $listProductShipment);

		//get document list for shipment
		$listDocumentShipment = $documentTable->getList($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_DOCUMENT_ITEM, $listDocumentShipment);

		//get sensor list for shipment
		$listSensorShipment = $hardwareTable->getListByDestination($shipmentId, Sss_Ultility_SystemName::SHIPMENTS);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM, $listSensorShipment);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM_COUNT, count($listSensorShipment));

		//action button
		$actionbuttons = array(
		new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::PRINTF, 'shipment-detail-export?print=1&' . "shipmentid=$shipmentId", Resources::_('PRINT')),
		new Sss_CommonEntity_ActionButton(Sss_Ultility_ActionButtonType::BACK, 'shipment-history', Resources::_('BTN_BACK'))
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_BUTTONS, $actionbuttons);

		//get breadscrumb
		$breadscrumbs = array(
		new Sss_CommonEntity_BreadScrumb($this->_menuName, ''),
		new Sss_CommonEntity_BreadScrumb(Resources::_('SHIPMENT_DETAIL'), '#')
		);
		$this->view->assign(Sss_Ultility_CommonViewParamater::BREADSCRUMBS, $breadscrumbs);
	}
	
	public function shipmentDetailExportAction()
	{
		$systemTypeTable = new Shepherd_Model_Mapper_SystemType();
		$shipmentTable   = new Shepherd_Model_Mapper_Shipment();
		$alertTable      = new Shepherd_Model_Mapper_Alert();
		$productTable    = new Shepherd_Model_Mapper_Product();
		$documentTable   = new Shepherd_Model_Mapper_Document();
		$hardwareTable   = new Application_Model_Mapper_Hardware();


		//get shipment type
		$shipmenttypeId = $this->getRequest()->getParam('systemtypeid', -1);

		$this->view->assign('shipmenttype', $shipmenttypeId);

		$shipmentTypes = $systemTypeTable->getSystemBySystemName(Sss_Ultility_SystemName::SHIPMENT_TYPE);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_TYPES, $shipmentTypes);

		//get shipmentid
		$params     = $this->getRequest()->getPost();
		$shipmentId = $this->getRequest()->getParam('shipmentid', -1);
		if (($shipmentId == -1 || (int) $shipmentId == 0) && isset($params['shipmentid']))
			$shipmentId = $params['shipmentid'];
		$this->view->assign('shipmentid', $shipmentId);

		//get shipment by id
		$shipment = $shipmentTable->getShipmentById($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT, $shipment);

		//get alert list for shipment
		$listAlertShipment = $alertTable->getList(Sss_Ultility_SystemName::SHIPMENTS, $shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_ALERT_SHIPMENT, $listAlertShipment);

		//get product list for shipment
		$listProductShipment = $productTable->getList($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_PRODUCT_ITEM, $listProductShipment);

		//get document list for shipment
		$listDocumentShipment = $documentTable->getList($shipmentId);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_DOCUMENT_ITEM, $listDocumentShipment);

		//get sensor list for shipment
		$listSensorShipment = $hardwareTable->getListByDestination($shipmentId, Sss_Ultility_SystemName::SHIPMENTS);
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW_SENSOR_ITEM, $listSensorShipment);

		$queryString = '?shipmentid=' . $shipmentId;

		$this->view->assign(Sss_Ultility_CommonViewParamater::ACTION_COMMON, 'shipment-detail');
		$this->view->assign(Sss_Ultility_CommonViewParamater::QUERY_STRING, $queryString);
	}

	public function shipmentHistoryExportAction()
	{
		$shipmentTable = new Shepherd_Model_Mapper_Shipment();

		//get order, page, limit param
		$filterParam[Sss_Ultility_CommonViewParamater::ORDERFIELD] = isset($get[Sss_Ultility_CommonViewParamater::ORDERFIELD]) ? $get[Sss_Ultility_CommonViewParamater::ORDERFIELD] : null;
		$filterParam[Sss_Ultility_CommonViewParamater::ORDERTYPE]  = isset($get[Sss_Ultility_CommonViewParamater::ORDERTYPE]) ? $get[Sss_Ultility_CommonViewParamater::ORDERTYPE] : null;                
		$filterParam[Sss_Ultility_CommonViewParamater::PAGE] = isset($get[Sss_Ultility_CommonViewParamater::PAGE]) ? $get[Sss_Ultility_CommonViewParamater::PAGE] : 1;
		//test param filter: omit title text in field
		$paramOriginal        = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'origin', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$paramDestination     = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'destination', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$paramFromDate        = $this->getRequest()->getParam('date_from', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$paramToDate          = $this->getRequest()->getParam('date_to', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$paramInternalRef     = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'internal_ref', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$paramSupplierRef     = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'supplier_ref', Sss_Ultility_CommonViewParamater::UNDERFINE);
		$paramReferenceNumber = $this->getRequest()->getParam(Sss_Ultility_CommonViewParamater::FIX . 'reference_number', Sss_Ultility_CommonViewParamater::UNDERFINE);



		//get shipment filter
		$filterParam['origin']            = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'origin', $paramOriginal, Sss_Ultility_SessionName::SHIPMENT_SESSION);
		$filterParam['destination']      = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'destination', $paramDestination, Sss_Ultility_SessionName::SHIPMENT_SESSION);
		$filterParam['date_from']        = $this->getValueRequest('date_from', $paramFromDate, Sss_Ultility_SessionName::SHIPMENT_SESSION);
		$filterParam['date_to']          = $this->getValueRequest('date_to', $paramToDate, Sss_Ultility_SessionName::SHIPMENT_SESSION);
		$filterParam['internal_ref']     = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'internal_ref', $paramInternalRef, Sss_Ultility_SessionName::SHIPMENT_SESSION);
		$filterParam['supplier_ref']     = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'supplier_ref', $paramSupplierRef, Sss_Ultility_SessionName::SHIPMENT_SESSION);
		$filterParam['reference_number'] = $this->getValueRequest(Sss_Ultility_CommonViewParamater::FIX . 'reference_number', $paramReferenceNumber, Sss_Ultility_SessionName::SHIPMENT_SESSION);



		//get shipment history
			$filterParam['shipment_history'] = 1;
			$columnNames = array(
			Resources::_('ID'),
			Resources::_('NAME'),
			Resources::_('SHIPMENT_ORGIN_LIST'),
			Resources::_('SHIPMENT_DESTINATION_LIST'),
			Resources::_('DATE'),
			Resources::_('SHIPMENT_END_DATE')
			);
		$this->view->assign(Sss_Ultility_CommonViewParamater::SHIPMENT_HISTORY, $shipmentHistoryId);
		//get shipment list
		$list = $shipmentTable->getList(-1, 0, $filterParam);
		$rowAll  = $list->list;
		$pagination = $list->pagination;
		$this->view->assign(Sss_Ultility_CommonViewParamater::ROW, $rowAll); 
		$this->view->assign(Sss_Ultility_CommonViewParamater::PAGINATION, $pagination);
		$count = ((($pagination->pagecurrent * Sss_Ultility_Setting::PAGE_SIZE) - Sss_Ultility_Setting::PAGE_SIZE));
		$arrData = array();
		$index = 0;
		if($shipmentHistoryId != -1){
			foreach((array)$rowAll as $row){
				$count++;
				$arrData[$index][] = $count;
				$arrData[$index][] = $row['shipmentname'];
				$arrData[$index][] = $row['origin'];
				$arrData[$index][] = $row['destination'];
				$arrData[$index][] = Sss_Ultility_Common::getDateTime(DATETIMEFORMAT, $row['ship_date']);
				$arrData[$index][] = Sss_Ultility_Common::getDateTime(DATETIMEFORMAT, $row['end_date']);
				$index++;
			}
		}else{
			foreach((array)$rowAll as $row){
				$count++;
				$arrData[$index][] = $count;
				$arrData[$index][] = $row['shipmentname'];
				$arrData[$index][] = $row['origin'];
				$arrData[$index][] = $row['destination'];
				$arrData[$index][] = Sss_Ultility_Common::getDateTime(DATETIMEFORMAT, $row['ship_date']);
				$index++;
			}
		}
		if(isset($_GET['export']))
			$this->exportDataToExcel($columnNames, $arrData, Resources::_('SHIPMENT_HISTORY'));
	}
}